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:  Help! Trapped in the IO Monad! (Erik de Castro Lopo)
   2.  Monads... (Cory Knapp)
   3. Re:  Help! Trapped in the IO Monad! (Alexander Dunlap)
   4. Re:  Monads... (Erik de Castro Lopo)
   5. Re:  Help! Trapped in the IO Monad! (Erik de Castro Lopo)
   6. Re:  Monads... (nanothief)
   7. Re:  Help! Trapped in the IO Monad! (Erik de Castro Lopo)
   8. Re:  Monads... (Rafael Gustavo da Cunha Pereira Pinto)


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

Message: 1
Date: Thu, 29 Jan 2009 13:14:55 +1100
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Help! Trapped in the IO Monad!
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Alexander Dunlap wrote:

> It seems like foldM ought to do what you want. Could you post some
> more details please?

This is a function that I have working in Ocaml which is a little
more lenient about IO :-).

This is what I have but won't compile:

    fileNames :: ([FilePath] -> FilePath -> FilePath -> [FilePath])
                  -> [FilePath] -> FilePath -> IO [FilePath]
    fileNames builder builder_accum topdir = do
        names <- getDirectoryContents topdir
        let properNames = filter (`notElem` [".", ".."]) names
        (dirs, files) <- splitDirFile properNames
        let accum <- foldl' (\ acc f -> builder acc topdir f) builder_accum 
files
        return $ foldM (\ acc d -> fileNames builder accum (topdir </> d)) 
accum dirs

I get following error on the foldM:

    Couldn't match expected type `[FilePath]'
           against inferred type `IO [FilePath]'
      Expected type: IO [FilePath]
      Inferred type: IO (IO [FilePath])

Thinking about it some more, I can see the problem; accum is an
"IO [FilePath]" and my builder function requires a "[FilePath]".

Once I get the function working I would like to generalize it to:

    fileNames :: (a -> FilePath -> FilePath -> a) -> a -> FilePath -> IO a

Cheers,
Erik
-- 
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"Re graphics:  A picture is worth 10K words - but only those to
describe the picture.  Hardly any sets of 10K words can be
adequately described with pictures." -- Alan Perlis


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

Message: 2
Date: Wed, 28 Jan 2009 20:44:30 -0600
From: Cory Knapp <[email protected]>
Subject: [Haskell-beginners] Monads...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello, so, I'm having a simple problem with monads: I have no idea how 
to actually use them. I understand the category theory (or, at least 
well enough to be able to explain "what is a monad"); I understand the 
way to declare something as a monad instance, but I just don't get how 
to program with them. Can anyone provide me with, or direct me towards, 
some simple monads and some ways of using (for example) the monadic 
properties of lists?

Thanks,
Cory


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

Message: 3
Date: Wed, 28 Jan 2009 18:55:09 -0800
From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] Help! Trapped in the IO Monad!
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Jan 28, 2009 at 6:14 PM, Erik de Castro Lopo
<[email protected]> wrote:
> Alexander Dunlap wrote:
>
>> It seems like foldM ought to do what you want. Could you post some
>> more details please?
>
> This is a function that I have working in Ocaml which is a little
> more lenient about IO :-).
>
> This is what I have but won't compile:
>
>    fileNames :: ([FilePath] -> FilePath -> FilePath -> [FilePath])
>                  -> [FilePath] -> FilePath -> IO [FilePath]
>    fileNames builder builder_accum topdir = do
>        names <- getDirectoryContents topdir
>        let properNames = filter (`notElem` [".", ".."]) names
>        (dirs, files) <- splitDirFile properNames
>        let accum <- foldl' (\ acc f -> builder acc topdir f) builder_accum 
> files
>        return $ foldM (\ acc d -> fileNames builder accum (topdir </> d)) 
> accum dirs
>
> I get following error on the foldM:
>
>    Couldn't match expected type `[FilePath]'
>           against inferred type `IO [FilePath]'
>      Expected type: IO [FilePath]
>      Inferred type: IO (IO [FilePath])
>
> Thinking about it some more, I can see the problem; accum is an
> "IO [FilePath]" and my builder function requires a "[FilePath]".
>
> Once I get the function working I would like to generalize it to:
>
>    fileNames :: (a -> FilePath -> FilePath -> a) -> a -> FilePath -> IO a
>
> Cheers,
> Erik
> --
> -----------------------------------------------------------------
> Erik de Castro Lopo

Try removing the "return $" on the last line. foldM ... will already
be in the IO monad; return will lift the IO a into the IO monad again,
so you'll have IO (IO a), which you don't want.

Alex


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

Message: 4
Date: Thu, 29 Jan 2009 13:57:02 +1100
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Monads...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Cory Knapp wrote:

> Hello, so, I'm having a simple problem with monads: I have no idea how 
> to actually use them. I understand the category theory (or, at least 
> well enough to be able to explain "what is a monad");

I come from the complete other end of the spectrum, someone who does
not know category theory, but know many programming languages, including
Ocaml function language much like Haskell, but less pure. It is possible
to use monads in Ocaml, but most code doesn't.

> I understand the 
> way to declare something as a monad instance, but I just don't get how 
> to program with them.

When it comes to coding with Haskell, you can basically ignore monads
and just follow the type signatures.

> Can anyone provide me with, or direct me towards, 
> some simple monads and some ways of using (for example) the monadic 
> properties of lists?

Writing my own monad is something I will tackle when I know I need to.

Erik
-- 
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"C++ is like jamming a helicopter inside a Miata and expecting
some sort of improvement." -- Drew Olbrich


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

Message: 5
Date: Thu, 29 Jan 2009 14:00:07 +1100
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Help! Trapped in the IO Monad!
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Alexander Dunlap wrote:

> Try removing the "return $" on the last line. foldM ... will already
> be in the IO monad; return will lift the IO a into the IO monad again,
> so you'll have IO (IO a), which you don't want.

Fantastic! Worked a treat.

Thanks,
Erik
-- 
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"C++ has its place in the history of programming languages. Just
as Caligula has his place in the history of the Roman Empire."
-- Robert Firth


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

Message: 6
Date: Thu, 29 Jan 2009 13:38:38 +1000
From: nanothief <[email protected]>
Subject: Re: [Haskell-beginners] Monads...
To: Cory Knapp <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Cory Knapp wrote:
> Hello, so, I'm having a simple problem with monads: I have no idea how 
> to actually use them. I understand the category theory (or, at least 
> well enough to be able to explain "what is a monad"); I understand the 
> way to declare something as a monad instance, but I just don't get how 
> to program with them. Can anyone provide me with, or direct me 
> towards, some simple monads and some ways of using (for example) the 
> monadic properties of lists?
>
> Thanks,
> Cory
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
I found that http://www.haskell.org/all_about_monads/html/index.html 
gave a lot of nice examples of using list and maybe monads. The list 
monad is particularly useful for finding possible solutions given 
available input values.
For example, with the problem
x + 8y = 114
3x - 8y + 4z = 182
x < y < z < 100
Find solutions for x,y,z

The program:
res :: [(Int,Int,Int)]
res = do
  x <- [1..100]
  y <- [1..100]
  z <- [1..100]
  guard $ x + 8 * y == 114
  guard $ 3*x - 8*y + 4*z == 182
  guard $ x < y
  guard $ y < z
  return (x,y,z)

will output all the possible solutions. Note how close the program is to 
the actual problem. The values of x,y, and z are chosen from the value 
[1..100], but if a guard statement fails, the (x,y,z) choice is abandoned.

Another example (taken from 
http://www.mathsisfun.com/puzzles/sum-of-digits-is-43-solution.html )
*The Puzzle:* I am thinking of a 6-digit number. The sum of the digits 
is 43.

And only two of the following three statements about the number are true:

(1) it's a square number,
(2) it's a cube number, and
(3) the number is under 500000.

the program
answer = do
  d1 <- [0..9]
  d2 <- [0..9]
  d3 <- [0..9]
  d4 <- [0..9]
  d5 <- [0..9]
  d6 <- [0..9]
  let digitSum = d1 + d2 + d3 + d4 + d5 + d6
  let value = d1 + d2*10 + d3*100 + d4*1000 + d5*10000 + d6*100000
  guard $ digitSum == 43
  let lessThan500000 = digitSum < 500000
  let isSquare = (round $ sqrt (fromIntegral value)) ^ 2 == value
  let isCube = (round $ (fromIntegral value) ** (1/3)) ^ 3 == value
  guard $ length (filter id [lessThan500000,isSquare,isCube]) == 2
  return value

will output the three answers (not that the author only found one 
solution!).
 




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

Message: 7
Date: Thu, 29 Jan 2009 14:46:50 +1100
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Help! Trapped in the IO Monad!
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Alexander Dunlap wrote:

> You might also look at Control.Monad.filterM. I often define a
> function "partitionM" which is like partition except it uses a monadic
> test, just like you have.

Just for completeness, my partitionM:

  partitionM :: (Monad m) => (a -> m Bool) -> [a] -> m ([a], [a])
  partitionM _ []     =  return ([], [])
  partitionM p (x:xs) =  do
      flg <- p x
      (ys, ns) <- partitionM p xs
      return (if flg then (x:ys, ns) else (ys, x:ns))

Cheers,
Erik
-- 
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"Indeed, I am impressed that Google runs an 8,000 node Linux
cluster, 5 data centers, an extensive network, and a rapidly
evolving application all with a staff of 12."
  -- http://research.microsoft.com/~gray/papers/FAAMs_HPTS.doc


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

Message: 8
Date: Thu, 29 Jan 2009 07:56:55 -0200
From: Rafael Gustavo da Cunha Pereira Pinto
        <[email protected]>
Subject: Re: [Haskell-beginners] Monads...
To: nanothief <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Cory,

The big hit for me was Phillip Wadler's paper "Monads for functional
programming" I made me start thinking "well, this looks like a monad..."

homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf

Cheers

On Thu, Jan 29, 2009 at 01:38, nanothief <[email protected]> wrote:

> Cory Knapp wrote:
>
>> Hello, so, I'm having a simple problem with monads: I have no idea how to
>> actually use them. I understand the category theory (or, at least well
>> enough to be able to explain "what is a monad"); I understand the way to
>> declare something as a monad instance, but I just don't get how to program
>> with them. Can anyone provide me with, or direct me towards, some simple
>> monads and some ways of using (for example) the monadic properties of lists?
>>
>> Thanks,
>> Cory
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
> I found that http://www.haskell.org/all_about_monads/html/index.html gave
> a lot of nice examples of using list and maybe monads. The list monad is
> particularly useful for finding possible solutions given available input
> values.
> For example, with the problem
> x + 8y = 114
> 3x - 8y + 4z = 182
> x < y < z < 100
> Find solutions for x,y,z
>
> The program:
> res :: [(Int,Int,Int)]
> res = do
>  x <- [1..100]
>  y <- [1..100]
>  z <- [1..100]
>  guard $ x + 8 * y == 114
>  guard $ 3*x - 8*y + 4*z == 182
>  guard $ x < y
>  guard $ y < z
>  return (x,y,z)
>
> will output all the possible solutions. Note how close the program is to
> the actual problem. The values of x,y, and z are chosen from the value
> [1..100], but if a guard statement fails, the (x,y,z) choice is abandoned.
>
> Another example (taken from
> http://www.mathsisfun.com/puzzles/sum-of-digits-is-43-solution.html )
> *The Puzzle:* I am thinking of a 6-digit number. The sum of the digits is
> 43.
>
> And only two of the following three statements about the number are true:
>
> (1) it's a square number,
> (2) it's a cube number, and
> (3) the number is under 500000.
>
> the program
> answer = do
>  d1 <- [0..9]
>  d2 <- [0..9]
>  d3 <- [0..9]
>  d4 <- [0..9]
>  d5 <- [0..9]
>  d6 <- [0..9]
>  let digitSum = d1 + d2 + d3 + d4 + d5 + d6
>  let value = d1 + d2*10 + d3*100 + d4*1000 + d5*10000 + d6*100000
>  guard $ digitSum == 43
>  let lessThan500000 = digitSum < 500000
>  let isSquare = (round $ sqrt (fromIntegral value)) ^ 2 == value
>  let isCube = (round $ (fromIntegral value) ** (1/3)) ^ 3 == value
>  guard $ length (filter id [lessThan500000,isSquare,isCube]) == 2
>  return value
>
> will output the three answers (not that the author only found one
> solution!).
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Rafael Gustavo da Cunha Pereira Pinto
Electronic Engineer, MSc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090129/5f9caad5/attachment.htm

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

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


End of Beginners Digest, Vol 7, Issue 24
****************************************

Reply via email to