Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 522533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:21:50+00:00 2026-05-13T08:21:50+00:00

I’m new to haskell, and i read through and digested Learn You A Haskell

  • 0

I’m new to haskell, and i read through and digested Learn You A Haskell For Great Good, trying out a couple of things along the way. For my first project i wanted to try the classic: FizzBuzz. So i came up with the following code:

import System.IO

fizzBuzz :: (Integral a) => a -> String
fizzBuzz num
    | fizz && buzz = "FizzBuzz"
    | fizz = "Fizz"
    | buzz = "Buzz"
    | otherwise = show num
    where fizz = num `mod` 3 == 0
          buzz = num `mod` 5 == 0

main = print $ map fizzBuzz [1..100]

Worked great, except i got a rather dense looking list that was hard to read. So i tried this main function instead:

main = map putStrLn $ map fizzBuzz [1..100]

And that gives me the error Couldn't match expected type 'IO t' against inferred type '[IO ()]'. I tried half a dozen things and none of it seemed to help. What’s the proper way to do what i’m trying to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:21:51+00:00Added an answer on May 13, 2026 at 8:21 am
    map :: (a -> b) -> [a] -> [b]
    putStrLn :: Show a => a -> IO ()
    map putStrLn :: Show a => [a] -> [IO ()]
    

    You’ve got a list of IO () actions.

    main :: IO ()
    

    You need to join them into a single IO () action.

    What you want to do is to perform each of those IO () actions in sequence/sequence_:

    sequence :: Monad m => [m a] -> m [a]
    sequence_ :: Monad m => [m a] -> m ()
    

    For convenience, mapM/mapM_ will map a function over a list and sequence the resulting monadic results.

    mapM :: Monad m => (a -> m b) -> [a] -> m [b]
    mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
    

    So your fixed code would look like this:

    main = mapM_ putStrLn $ map fizzBuzz [1..100]
    

    Although I’d probably write it like this:

    main = mapM_ (putStrLn . fizzBuzz) [1..100]
    

    Or even this:

    main = putStr $ unlines $ map fizzBuzz [1..100]
    

    Let’s write our own sequence. What do we want it to do?

    sequence [] = return []
    sequence (m:ms) = do
        x <- m
        xs <- sequence ms
        return $ x:xs
    
    • If there’s nothing left in the list, return (inject into the monad) an empty list of results.
    • Otherwise, within the monad,
      • Bind (for the IO monad, this means execute) the first result.
      • sequence the rest of the list; bind that list of results.
      • Return a cons of the first result and the list of other results.

    GHC’s library uses something more like foldr (liftM2 (:)) (return []) but that’s harder to explain to a newcomer; for now, just take my word that they’re equivalent.

    sequence_ is easier, since it doesn’t bother keeping track of the results. GHC’s library implements it as sequence_ ms = foldr (>>) (return ()) ms. Let’s just expand the definition of foldr:

      sequence [a, b, c, d]
    = foldr (>>) (return ()) [a, b, c, d]
    = a >> (b >> (c >> (d >> return ())))
    

    In other words, “do a, discard the result; do b; discard the result, … finally, return ()“.

    mapM  f xs = sequence  $ map f xs
    mapM_ f xs = sequence_ $ map f xs
    

    On the other hand, you don’t even need to know monads at all with the alternate unlines solution.

    What does unlines do? Well, lines "a\nb\nc\nd\n" = ["a", "b", "c", "d"], so of course unlines ["a", "b", "c", "d"] = "a\nb\nc\nd\n".

    unlines $ map fizzBuzz [1..100] = unlines ["1", "2", "Fizz", ..] = "1\n2\nFizz\n..." and off it goes to putStr. Thanks to the magic of Haskell’s laziness, the full string never needs to be constructed in memory, so this will happily go to [1..1000000] or higher 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.