Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.haskell > #512

Re: Get mapM to be lazy for IO

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.haskell
Subject Re: Get mapM to be lazy for IO
Date 2018-12-12 19:29 -0800
Organization A noiseless patient Spider
Message-ID <87wooep0i9.fsf@nightsong.com> (permalink)
References <24548724-4bdd-402d-8a76-1403b7e87169@googlegroups.com> <87bm5s4249.fsf@ixod.org> <a9bc88e6-c0cc-4de0-85a0-32e76dc4eb17@googlegroups.com>

Show all headers | View raw


Kaylen Wheeler <kfjwheeler@gmail.com> writes:
>            mapM (\n -> do
>                     putStrLn $"Input number " ++ show n ++ ": "
>                     l <- getLine
>                     return l)
>            [1..3]

That mapM runs the three actions and binds them together.  It's just
like mapM_ except it returns the result list.  So you run all three
actions before looking for "done".

>  mapM putStrLn $ words

In principle you want mapM_ there, though it won't matter.

This seems to work:

main = do
  putStrLn "Hello.  Please enter words until 'done'."
  let go [] xs = return xs
      go (n:ns) xs = do
        putStrLn $"Input number " ++ show n ++ ": "
        l <- getLine
        if l == "done" then return xs
          else go ns (l:xs)

  words <- reverse `fmap` go [1..3] []

  putStrLn "Here are the words:"
  mapM_ putStrLn $ words

Back to comp.lang.haskell | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Get mapM to be lazy for IO kfjwheeler@gmail.com - 2018-12-11 05:19 -0800
  Re: Get mapM to be lazy for IO Mark Carroll <mtbc@bcs.org> - 2018-12-11 13:36 +0000
    Re: Get mapM to be lazy for IO Kaylen Wheeler <kfjwheeler@gmail.com> - 2018-12-12 17:52 -0800
      Re: Get mapM to be lazy for IO Paul Rubin <no.email@nospam.invalid> - 2018-12-12 19:29 -0800
    Re: Get mapM to be lazy for IO Kaylen Wheeler <kfjwheeler@gmail.com> - 2018-12-12 18:21 -0800
      Re: Get mapM to be lazy for IO Paul Rubin <no.email@nospam.invalid> - 2018-12-12 19:31 -0800
  Re: Get mapM to be lazy for IO Barry Fishman <barry@ecubist.org> - 2018-12-13 09:52 -0500
    Re: Get mapM to be lazy for IO Barry Fishman <barry@ecubist.org> - 2018-12-13 10:21 -0500
      Re: Get mapM to be lazy for IO Barry Fishman <barry@ecubist.org> - 2018-12-13 10:46 -0500

csiph-web