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: lazy mapM (Kim-Ee Yeoh)
2. Re: lazy mapM (Ovidiu D)
3. Re: lazy mapM (David McBride)
4. Re: lazy mapM (Ovidiu D)
5. Re: lazy mapM (David McBride)
----------------------------------------------------------------------
Message: 1
Date: Mon, 1 Apr 2013 07:29:54 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] lazy mapM
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<capy+zdtoch6rdthcduksucqspunrt14axma_uvuoxxtbbl-...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Mon, Apr 1, 2013 at 6:26 AM, Ovidiu D <[email protected]> wrote:
> 1. Make f behave lazy
> Its input list is made of lines read from stdin and I want it to process
> lines one by one as they are entered by the user.
Eschewing laziness (which adds only complexity in this case), here's
something that'll work, if a little ugly:
import System.Exit
f :: String -> IO ()
f "exit" = exitSuccess
f a = putStrLn $ "you entered: " ++ a
main = do
s <- getLine
f s
main
Going down this path would involve IORef's, among others from the "sin bin".
Something more pure and haskell-y would typically involve an analysis
of the DSL abstract syntax and state space and implementation using a
combination of State and Free monads.
-- Kim-Ee
------------------------------
Message: 2
Date: Mon, 1 Apr 2013 03:49:07 +0300
From: Ovidiu D <[email protected]>
Subject: Re: [Haskell-beginners] lazy mapM
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<CAKVsE7uFUQ6Nvwht5XvcTg0fePFkcgy5nm9=qsozkcgu8k4...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Thanks. Since this is an exercise I would be really curious what is the
idiomatic Haskell approach.
On Mon, Apr 1, 2013 at 3:29 AM, Kim-Ee Yeoh <[email protected]> wrote:
> On Mon, Apr 1, 2013 at 6:26 AM, Ovidiu D <[email protected]> wrote:
> > 1. Make f behave lazy
> > Its input list is made of lines read from stdin and I want it to process
> > lines one by one as they are entered by the user.
>
> Eschewing laziness (which adds only complexity in this case), here's
> something that'll work, if a little ugly:
>
> import System.Exit
>
> f :: String -> IO ()
> f "exit" = exitSuccess
> f a = putStrLn $ "you entered: " ++ a
>
> main = do
> s <- getLine
> f s
> main
>
> Going down this path would involve IORef's, among others from the "sin
> bin".
>
> Something more pure and haskell-y would typically involve an analysis
> of the DSL abstract syntax and state space and implementation using a
> combination of State and Free monads.
>
> -- Kim-Ee
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130401/89531d6f/attachment-0001.htm>
------------------------------
Message: 3
Date: Sun, 31 Mar 2013 20:53:04 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] lazy mapM
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<can+tr40a6mxmm6cqxptnxw_kujsexzbqleimgyhoy07vsvk...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
I know you have the best intentions in writing this, but there are
pitfalls. Unexpected things happen when you interleave IO in this manner,
but nonetheless, here's how you would do it.
myGetLine = do
x <- getLine
if (x == "exit")
then return []
else do
xs <- unsafeInterleaveIO myGetLine
return (x:xs)
main = do
x <- myGetLine
print x
Just know that at some point you should learn to use conduits or pipes for
a much better approach to modeling things like this.
On Sun, Mar 31, 2013 at 7:26 PM, Ovidiu D <[email protected]> wrote:
> Hi again,
>
> Given the following code:
>
> g :: IO String -> IO String
>
> f :: [IO String] -> IO [ String ]
> f = mapM g
>
> The implementation of f is wrong because I would like to:
> 1. Make f behave lazy
> Its input list is made of lines read from stdin and I want it to process
> lines one by one as they are entered by the user.
>
> 2. Implement f such that it stops consuming items from the input list
> when the input item meets some condition. For example:
> isExit item = ("exit" == item)
>
> I tried to implement my own custom iteration by recursion but I got stuck
> in the combination of IO and list monads.
>
> Any help is appreciated.
>
> Thanks!
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130331/534eb48b/attachment-0001.htm>
------------------------------
Message: 4
Date: Mon, 1 Apr 2013 04:38:56 +0300
From: Ovidiu D <[email protected]>
Subject: Re: [Haskell-beginners] lazy mapM
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<CAKVsE7vfVDLAo+O-X9qRB+EY6s=FEHZ1OYtu=zbptjydkhz...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
I'm not sure I understand what you mean by "I know you have the best
intentions in writing this, but there are pitfalls.". Anyway, here's the
code which doesn't work apparently because mapM is waiting for the whole
list before it goes further.
prompt = ">> "
commands :: [IO String]
commands = readCommand : commands
where readCommand = putStr prompt >> getLine
display :: Show a => [ a ] -> IO ()
display = mapM_ $ putStr . show
executeCommand :: String -> String
executeCommand = printf "Command not implemented: '%s'"
processCommands :: [IO String] -> IO [ String ]
processCommands = mapM processOneCommand
where processOneCommand cmd = cmd >>= (return . executeCommand )
main =
hSetBuffering stdout NoBuffering
>> processCommands commands
>>= display
This is just for learning purposes and I'm looking for the "haskell way to
do it". My intention is to write the function processCommands such that it
takes the decision to either fetch the next command from the command list
(i.e. console) or to exit the application.
Regarding your comment "Just know that at some point you should learn to
use conduits or pipes for a much better approach to modeling things like
this.". Can you point me to some documentation?
Thanks!
On Mon, Apr 1, 2013 at 3:53 AM, David McBride <[email protected]> wrote:
> I know you have the best intentions in writing this, but there are
> pitfalls. Unexpected things happen when you interleave IO in this manner,
> but nonetheless, here's how you would do it.
>
> myGetLine = do
> x <- getLine
> if (x == "exit")
> then return []
> else do
> xs <- unsafeInterleaveIO myGetLine
> return (x:xs)
>
> main = do
> x <- myGetLine
> print x
>
> Just know that at some point you should learn to use conduits or pipes for
> a much better approach to modeling things like this.
>
>
>
> On Sun, Mar 31, 2013 at 7:26 PM, Ovidiu D <[email protected]> wrote:
>
>> Hi again,
>>
>> Given the following code:
>>
>> g :: IO String -> IO String
>>
>> f :: [IO String] -> IO [ String ]
>> f = mapM g
>>
>> The implementation of f is wrong because I would like to:
>> 1. Make f behave lazy
>> Its input list is made of lines read from stdin and I want it to process
>> lines one by one as they are entered by the user.
>>
>> 2. Implement f such that it stops consuming items from the input list
>> when the input item meets some condition. For example:
>> isExit item = ("exit" == item)
>>
>> I tried to implement my own custom iteration by recursion but I got stuck
>> in the combination of IO and list monads.
>>
>> Any help is appreciated.
>>
>> Thanks!
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130401/c1636782/attachment-0001.htm>
------------------------------
Message: 5
Date: Sun, 31 Mar 2013 22:49:12 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] lazy mapM
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<can+tr43fcoulmegs1_bwragbwjrpyeceoakwm8q59npv7he...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Doing it the way you are trying to do it breaks the IO abstraction. In
order to do it you'd have to use unsafe functions. Unsafe functions are
bad. I'm not going to explain why but they tend to bite you as your
program gets more complex and weirdness starts to occur, like threads
ceasing operation while awaiting input is something that bit me when I went
down that route. So let me explain how I would do it using both pipes and
conduits as examples:
import Data.Conduit as C hiding ((>+>), runPipe)
import System.IO
import Control.Monad.Trans
import Text.Printf.Mauke
import Control.Pipe as P
import Control.Monad (forever)
-- Source runs in the IO monad and produces Strings
commandSource :: Source IO String
commandSource = do
command <- liftIO getLine
if command == "exit"
then return ()
else do
C.yield command
commandSource -- loop to fetching new values to send down the pipe
-- Sink runs in the IO monad and takes any printfable argument and returns
() when pipe completes.
displaySink :: PrintfArg a => Sink a IO ()
displaySink = do
m <- C.await
case m of
Nothing -> return () -- if nothing comes in, just exit
Just x -> do
liftIO $ printf "Command not implemented (conduit): '%s'\n" x
displaySink
main = do
hSetBuffering stdout NoBuffering
commandSource $$ displaySink
runPipe $ commandProducer >+> displayConsumer
commandProducer :: PrintfArg a => Producer a String IO ()
commandProducer = do
x <- lift getLine
if x == "exit"
then return ()
else P.yield x >> commandProducer
displayConsumer :: Consumer String IO ()
displayConsumer = forever $ P.await >>= lift . printf "Command not
implemented (pipes): '%s'\n"
There are some utility function to shorten some of these definitions a bit
in conduit. These two examples are equivalent. But basically you are
creating a pipeline, the first of which gets commands until it gets an exit
and then sends them down the pipeline (as a string). The second piece of
the pipe accepts anything that is printfable and prints it. It will stop
when the upstream stops sending it strings to print. The point here is
that you have little functions that you can compose together with other
functions and create something bigger where none of the pieces interfere
with each other or break the IO abstraction.
As to which of these libraries you should try? Conduits is a bit more
straight forward and has a lot more documentation and supporting
libraries. Pipes is a lot more flexible in that you could send things both
directions along the pipe in the future when you become proficient with the
library.
On Sun, Mar 31, 2013 at 9:38 PM, Ovidiu D <[email protected]> wrote:
> I'm not sure I understand what you mean by "I know you have the best
> intentions in writing this, but there are pitfalls.". Anyway, here's the
> code which doesn't work apparently because mapM is waiting for the whole
> list before it goes further.
>
> prompt = ">> "
>
> commands :: [IO String]
> commands = readCommand : commands
> where readCommand = putStr prompt >> getLine
>
> display :: Show a => [ a ] -> IO ()
> display = mapM_ $ putStr . show
>
> executeCommand :: String -> String
> executeCommand = printf "Command not implemented: '%s'"
>
> processCommands :: [IO String] -> IO [ String ]
> processCommands = mapM processOneCommand
> where processOneCommand cmd = cmd >>= (return . executeCommand )
>
> main =
> hSetBuffering stdout NoBuffering
> >> processCommands commands
> >>= display
>
> This is just for learning purposes and I'm looking for the "haskell way to
> do it". My intention is to write the function processCommands such that it
> takes the decision to either fetch the next command from the command list
> (i.e. console) or to exit the application.
>
> Regarding your comment "Just know that at some point you should learn to
> use conduits or pipes for a much better approach to modeling things like
> this.". Can you point me to some documentation?
>
> Thanks!
>
>
> On Mon, Apr 1, 2013 at 3:53 AM, David McBride <[email protected]> wrote:
>
>> I know you have the best intentions in writing this, but there are
>> pitfalls. Unexpected things happen when you interleave IO in this manner,
>> but nonetheless, here's how you would do it.
>>
>> myGetLine = do
>> x <- getLine
>> if (x == "exit")
>> then return []
>> else do
>> xs <- unsafeInterleaveIO myGetLine
>> return (x:xs)
>>
>> main = do
>> x <- myGetLine
>> print x
>>
>> Just know that at some point you should learn to use conduits or pipes
>> for a much better approach to modeling things like this.
>>
>>
>>
>> On Sun, Mar 31, 2013 at 7:26 PM, Ovidiu D <[email protected]> wrote:
>>
>>> Hi again,
>>>
>>> Given the following code:
>>>
>>> g :: IO String -> IO String
>>>
>>> f :: [IO String] -> IO [ String ]
>>> f = mapM g
>>>
>>> The implementation of f is wrong because I would like to:
>>> 1. Make f behave lazy
>>> Its input list is made of lines read from stdin and I want it to process
>>> lines one by one as they are entered by the user.
>>>
>>> 2. Implement f such that it stops consuming items from the input list
>>> when the input item meets some condition. For example:
>>> isExit item = ("exit" == item)
>>>
>>> I tried to implement my own custom iteration by recursion but I got
>>> stuck in the combination of IO and list monads.
>>>
>>> Any help is appreciated.
>>>
>>> Thanks!
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130331/c6c1c083/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 58, Issue 2
****************************************