Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  define action getInt like getLine (Daniel Fischer)
   2.  (Integral b, RealFrac b) to Int (han)
   3.  Re: (Integral b, RealFrac b) to Int (han)
   4. Re:  Re: (Integral b, RealFrac b) to Int (David Virebayre)
   5.  Filtering lists ([email protected])
   6.  Re: define action getInt like getLine (Christian Maeder)
   7. Re:  Re: (Integral b, RealFrac b) to Int (Daniel Fischer)
   8. Re:  Filtering lists (Stephen Tetley)
   9. Re:  Filtering lists (Daniel Fischer)
  10.  Simplifying code (edgar klerks)


----------------------------------------------------------------------

Message: 1
Date: Mon, 8 Feb 2010 22:58:56 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Montag 08 Februar 2010 22:43:01 schrieb [email protected]:
> but I have to write the action
> readMonth :: IO Month
> on my own and still don't have an idea how to do it

Go take a look at

http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Prelude.html#v%3Aread
http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Prelude.html#v%3AreadLn
http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Prelude.html#v%3AgetLine

The documentation should help you.




------------------------------

Message: 2
Date: Tue, 9 Feb 2010 16:40:57 +0900
From: han <[email protected]>
Subject: [Haskell-beginners] (Integral b, RealFrac b) to Int
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I have this code:

line (sx, sy) (tx, ty)
        | abs (sx - tx) > abs (sy - ty) =
                zip xs [(sy + (sy - ty) * x / xd) | x <- [0 .. xd]]
        | otherwise =
                zip [(sx + (sx - tx) * y / yd) | y <- [0 .. yd]] ys
        where
                xs = target sx tx
                ys = target sy ty
                xd = abs (sx - tx)
                yd = abs (sy - ty)

It currently has the inferred type signature of

(Integral b, RealFrac b) => (b, b) -> (b, b) -> [(b, b)]

and I want it to be

(Int, Int) -> (Int, Int) -> [(Int, Int)]

which, when coerced, causes an error.

Any idea?


------------------------------

Message: 3
Date: Tue, 9 Feb 2010 16:53:13 +0900
From: han <[email protected]>
Subject: [Haskell-beginners] Re: (Integral b, RealFrac b) to Int
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I made a mistake in copying the original code.

The correct version is:

line (sx, sy) (tx, ty)
        | abs (sx - tx) > abs (sy - ty) =
                zip xs [round (sy + (sy - ty) * x / xd) | x <- [0 .. xd]]
        | otherwise =
                zip [round (sx + (sx - tx) * y / yd) | y <- [0 .. yd]] ys
        where
                xs = target sx tx
                ys = target sy ty
                xd = abs (sx - tx)
                yd = abs (sy - ty)

On Tue, Feb 9, 2010 at 4:40 PM, han <[email protected]> wrote:
> I have this code:
>
> line (sx, sy) (tx, ty)
>        | abs (sx - tx) > abs (sy - ty) =
>                zip xs [(sy + (sy - ty) * x / xd) | x <- [0 .. xd]]
>        | otherwise =
>                zip [(sx + (sx - tx) * y / yd) | y <- [0 .. yd]] ys
>        where
>                xs = target sx tx
>                ys = target sy ty
>                xd = abs (sx - tx)
>                yd = abs (sy - ty)
>
> It currently has the inferred type signature of
>
> (Integral b, RealFrac b) => (b, b) -> (b, b) -> [(b, b)]
>
> and I want it to be
>
> (Int, Int) -> (Int, Int) -> [(Int, Int)]
>
> which, when coerced, causes an error.
>
> Any idea?
>


------------------------------

Message: 4
Date: Tue, 9 Feb 2010 08:59:21 +0100
From: David Virebayre <[email protected]>
Subject: Re: [Haskell-beginners] Re: (Integral b, RealFrac b) to Int
To: han <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Start by replacing    /     by     `div`

David.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100209/03773cd5/attachment-0001.html

------------------------------

Message: 5
Date: Tue, 9 Feb 2010 11:53:00 +0100 (CET)
From: [email protected]
Subject: [Haskell-beginners] Filtering lists
To: [email protected]
Message-ID:
        
<67097985.20958731265712780646.javamail.r...@zimbra10-e2.priv.proxad.net>
        
Content-Type: text/plain; charset=utf-8

Hi, 
i get a .csv file with two different record formats (too bad).
The first one, identified by a "A" in the second field, contains 4 fields.
The second one, identified by a "B", contains 10 fields.

In order to put them in lists, i use lines then break to get a list of lists.
Then i want to filter this list, one for the A list, the other one for the B 
list.
Unfortunately, the  pattern for the A type contains 4 fields; when a type B 
list occurs (10 fields), i get a message concerning the pattern : 
Non-exhaustive patterns in lambda

How can i separate the two record formats?

Thanks, 
Didier


------------------------------

Message: 6
Date: Tue, 09 Feb 2010 12:49:49 +0100
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: define action getInt like getLine
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

[email protected] schrieb:
> but I have to write the action
> readMonth :: IO Month
> on my own and still don't have an idea how to do it

Your idea (below) to use show on all possible values and compare it to
the input line isn't that bad.

>> On 8 February 2010 21:19,  <[email protected]> wrote:
>>> I want to read a "Month" from input and if this month issn't declared in
>> my data type "Month" I want to throw an error message.
>>> data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct |
>> Nov | Dec deriving (Eq,Enum,Show)

The Enum class allows you to create the list of all possible values
 [Jan .. Dec]

This list can be turn into a lookup-list (for Data.List.lookup) by
  map (\ a -> (show a, a))

>>>>> readMyDatatype = do
>>>>>   if readLn == (show readLn)
>>>>>   then return readLn
>>>>>   else do error "input error"

This code is unfortunate, because "readLn" is an IO-Action (that cannot
be compared or shown). Even if you insert as first line:

   readLn <- getLine

readLn is a String that will never be equal to "show readLn", because
show would add the double quotes. Also this shadowing of "readLn" is no
good practise and should be avoided, so better use:

  str <- getLine

and use the string "str" to look it up in the list above.

Later on you may generalize readMonth to:

readMyDatatype :: (Show a, Enum a, Bounded a) => IO a

and also trim leading and trailing white space and case differences.

Cheers Christian

P.S. The functions read, readIO and readLn all have disadvantages in
case of errors. In the spirit of readIO it is possible to program
readMaybe, readEither or readM that are far more useful and missed very
often. Many such variants are somewhere, but one of readMaybe,
readEither or readM should be in a standard library.

readM would not be a generalization of readIO! But readIO could be
expressed using readM.



------------------------------

Message: 7
Date: Tue, 9 Feb 2010 13:23:50 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: (Integral b, RealFrac b) to Int
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Dienstag 09 Februar 2010 08:53:13 schrieb han:
> I made a mistake in copying the original code.
>
> The correct version is:
>
> line (sx, sy) (tx, ty)
>
>       | abs (sx - tx) > abs (sy - ty) =
>
>               zip xs [round (sy + (sy - ty) * x / xd) | x <- [0 .. xd]]
>
>       | otherwise =
>
>               zip [round (sx + (sx - tx) * y / yd) | y <- [0 .. yd]] ys
>       where
>               xs = target sx tx
>               ys = target sy ty
>               xd = abs (sx - tx)
>               yd = abs (sy - ty)

The type of (/) is

Prelude> :t (/)
(/) :: (Fractional a) => a -> a -> a

and that of round

Prelude> :t round
round :: (RealFrac a, Integral b) => a -> b

So the expression

(sy + (sy - ty) * x / xd)

forces sy, ty and xd to have the same Fractional type, fr. 
Since xd = abs (sx-tx), sx and tx have the same type.

Now you apply round to that expression, which can give you any Integral 
type you desire. In the first branch, round (...) becomes the second 
component of the result pairs, in the second branch the first component.

Probably target has type
target :: (Num/Integral/? a) => a -> a -> [a]

, which forces all types to be the same, giving the overall type

line :: (Integral b, RealFrac b) => (b, b) -> (b, b) -> [(b, b)]

Usually, no type is an instance of Integral and RealFrac at the same time, 
so it's not a usable function.

You want the type

line :: (Int,Int) -> (Int,Int) -> [(Int,Int)]

One way to find out what went wrong is to give that type signature in the 
source and see what error message you get.
It will probably be

    No instance for (Fractional Int)
      arising from a use of `/' at (source location)
    Possible fix: add an instance declaration for (Fractional Int)
    In the expression: ...

So you can't divide Ints with (/). You can either use the integral division 
"div": (sy + (sy - ty) * x `div` xd), or, if the round is important and you 
can't use integral division - which gives floor -, convert the arguments to 
(/) to a Fractional type before dividing:
(sy + round (fromIntegral ((sy - ty) * x) / fromIntegral xd))
>
> On Tue, Feb 9, 2010 at 4:40 PM, han <[email protected]> wrote:
> > I have this code:
> >
> > line (sx, sy) (tx, ty)
> >        | abs (sx - tx) > abs (sy - ty) =
> >                zip xs [(sy + (sy - ty) * x / xd) | x <- [0 .. xd]]
> >        | otherwise =
> >                zip [(sx + (sx - tx) * y / yd) | y <- [0 .. yd]] ys
> >        where
> >                xs = target sx tx
> >                ys = target sy ty
> >                xd = abs (sx - tx)
> >                yd = abs (sy - ty)
> >
> > It currently has the inferred type signature of
> >
> > (Integral b, RealFrac b) => (b, b) -> (b, b) -> [(b, b)]
> >
> > and I want it to be
> >
> > (Int, Int) -> (Int, Int) -> [(Int, Int)]
> >
> > which, when coerced, causes an error.
> >
> > Any idea?




------------------------------

Message: 8
Date: Tue, 9 Feb 2010 12:27:50 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Filtering lists
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello

If you have a list of String for each record, use partition from
Data.List, e.g.:


import Data.List ( partition )

partitionBySize :: [[String]] -> ([[String]],[[String]])
partitionBySize = partition (\xs -> length xs < 10)

-- for demo-ing:

printShortsLongs ::  ([[String]],[[String]]) -> IO ()
printShortsLongs (xs,ys) = do
  putStrLn "Shorts:"
  mapM_ print xs
  putStrLn "Longs:"
  mapM_ print ys

demo1 = printShortsLongs $ partitionBySize [
    ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
  , ["1", "2", "3", "4"]
  , ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
  , ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
  , ["1", "2", "3", "4"]
  ]



On 9 February 2010 10:53,  <[email protected]> wrote:
> Hi,
> i get a .csv file with two different record formats (too bad).
> The first one, identified by a "A" in the second field, contains 4 fields.
> The second one, identified by a "B", contains 10 fields.
>
> In order to put them in lists, i use lines then break to get a list of lists.
> Then i want to filter this list, one for the A list, the other one for the B 
> list.
> Unfortunately, the  pattern for the A type contains 4 fields; when a type B 
> list occurs (10 fields), i get a message concerning the pattern : 
> Non-exhaustive patterns in lambda
>
> How can i separate the two record formats?
>
> Thanks,
> Didier
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


------------------------------

Message: 9
Date: Tue, 9 Feb 2010 13:35:54 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Filtering lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Dienstag 09 Februar 2010 11:53:00 schrieb [email protected]:
> Hi,
> i get a .csv file with two different record formats (too bad).
> The first one, identified by a "A" in the second field,

Couldn't that at least be the first field?

> contains 4 fields. The second one, identified by a "B",
> contains 10 fields.
>
> In order to put them in lists, i use lines then break to get a list of
> lists. Then i want to filter this list, one for the A list, the other
> one for the B list. Unfortunately, the  pattern for the A type contains
> 4 fields; when a type B list occurs (10 fields), i get a message
> concerning the pattern : Non-exhaustive patterns in lambda
>
> How can i separate the two record formats?

data Rec
    = A a1 a2 a3 a4
    | B b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
      deriving (whatever)

isA :: Rec -> Bool
isA (A _ _ _ _) = True
isA _ = False

breakIntoFields :: String -> [[String]]
breakIntoFields file = map (splitOn ',') $ lines file

recordToRec :: [String] -> Rec
recordToRec (a1 : "A" : a2 : a3 : : a4 []) 
    = (A (read a1) (read a2) (read a3) (read a4))
recordToRec (b1 : "B" : ... )
    = (B (read b1) ...)
recordToRec _ = error "invalid record format"

records :: String -> [Rec]
records = map recordToRec . breakIntoFields

solution = Data.List.partition isA . records

>
> Thanks,
> Didier


------------------------------

Message: 10
Date: Tue, 9 Feb 2010 21:11:55 +0100
From: edgar klerks <[email protected]>
Subject: [Haskell-beginners] Simplifying code
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi All,

I wrote a program which permutates a list words with some rules. For example
if I feed it a list like:

banana
waterloo
vraag

It gives back the list:

banana
b...@nana
b...@na
b...@n@na
banan@
b...@nan@
b...@n@
b...@n@n@
waterloo
wa+erloo
water|oo
waterl0o
etc

However I have the feeling I am doing things to complicated. I am still a
beginner. Would someone like to help me simplify somethings. If you think
this is inappropriate please state also. I am not offended then. I
understand you are offering your spare time to help me.

The first thing I don't get is this. I recognize some things could be
rewritten with a bind operator (because of the concat $ fmap), but I am
puzzled how:

mutateWords :: [Char] -> [[Char]]
mutateWords word = nub.concat $ fmap snd <$> fmap unzip <$> ( foldr(\x z ->

                        let char = snd x
                            nm = number word
                            lst = fst x
                        in (insertAt char nm <$> lst) : z
                ) [[]] $ mw word )




Here is the full code:


import Data.List
import System
import System.IO
import Control.Applicative


---CONFIG section

leat = ['s' ==> '$', 't' ==> '+', 'l' ==> '|', 'o' ==> '0','e' ==> '3', 'a'
==> '@', 'v' ==> '^']

leata = fst.unzip $ leat
leatb = snd.unzip $ leat

-- Perl like assoc lists
infixl 1 ==>
a ==> b = (a, b)


-- Flipped fmap sometimes nicer
infixl 4 <$$>

xs <$$> f = f <$> xs


-- first I need to find  the positions of the mutatable charachters.
findPositions :: [Char] -> [[Int]]
findPositions xs = take (length index) $ index <*> [xs]
        where index = elemIndices <$> leata

-- And generate all subsequences
findSubSeq :: [Char] -> [[[Int]]]
findSubSeq  = fmap subsequences <$> findPositions


-- Only change elements which needs to be changed
insertAt :: Char -> [(Int, Char)] -> [Int] -> [(Int,Char)]
insertAt c xs ps = xs <$$> (\x ->
                if (fst x) `elem` ps
                        then (fst x , c)
                        else x
                )
-- Couples character to mutable positions
mw word = (findSubSeq word) `zip` leatb

number = zip [0..]

mutateWords :: [Char] -> [[Char]]
mutateWords word = nub.concat $ fmap snd <$> fmap unzip <$> ( foldr(\x z ->

                        let char = snd x
                            nm = number word
                            lst = fst x
                        in (insertAt char nm <$> lst) : z
                ) [[]] $ mw word )

generateAll :: [Char] -> [[Char]]
generateAll word = g lea $ mutateWords word
    where   g 0 words = words
                g n words = g (n - 1) (nub  $  words >>= mutateWords )
                lea = length leata
main = do
        filename <- getArgs
        wordlist <- readFile $ filename !! 0
        let a = (words wordlist) >>= generateAll
        mapM_ putStrLn  a

-- 
Flatliner ICT Service,
Email: [email protected],
Tel: +31727851429
Fax: +31848363080
Skype: edgar.klerks
Website: flatlinerict.nl
Adres: Koelmalaan 258,
1813JD, Alkmaar
Nederland
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100209/363c8357/attachment.html

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 20, Issue 15
*****************************************

Reply via email to