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 8292979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:40:35+00:00 2026-06-08T13:40:35+00:00

I’m new to functional programming and recently learning at Learn You a Haskell ,

  • 0

I’m new to functional programming and recently learning at Learn You a Haskell, but when I went through this chapter, I got stuck with the program below:

import Control.Monad.Writer  

logNumber :: Int -> Writer [String] Int  
logNumber x = Writer (x, ["Got number: " ++ show x])  

multWithLog :: Writer [String] Int  
multWithLog = do  
    a <- logNumber 3  
    b <- logNumber 5  
    return (a*b)

I saved these lines in a .hs file and but failed to import it into my ghci which complained:

more1.hs:4:15:
    Not in scope: data constructor `Writer'
    Perhaps you meant `WriterT' (imported from Control.Monad.Writer)
Failed, modules loaded: none.

I examined the type by “:info” command:

Prelude Control.Monad.Writer> :info Writer
type Writer w = WriterT w Data.Functor.Identity.Identity
               -- Defined in `Control.Monad.Trans.Writer.Lazy'

From my point of view, this was supposed to be something like “newtype Writer w a …”
so I’m confused about how to feed the data constructor and get a Writer.

I guess it might be a version-related problem and my ghci version is 7.4.1

  • 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-06-08T13:40:37+00:00Added an answer on June 8, 2026 at 1:40 pm

    The package Control.Monad.Writer does not export the data constructor Writer. I guess this was different when LYAH was written.

    Using the MonadWriter typeclass in ghci

    Instead, you create writers using the writer function. For example, in a ghci session I can do

    ghci> import Control.Monad.Writer
    ghci> let logNumber x = writer (x, ["Got number: " ++ show x])
    

    Now logNumber is a function that creates writers. I can ask for its type:

    ghci> :t logNumber
    logNumber :: (Show a, MonadWriter [String] m) => a -> m a
    

    Which tells me that the inferred type is not a function that returns a particular writer, but rather anything that implements the MonadWriter type class. I can now use it:

    ghci> let multWithLog = do { a <- logNumber 3; b <- logNumber 5; return (a*b) }
        :: Writer [String] Int
    

    (Input actually entered all on one line). Here I’ve specified the type of multWithLog to be Writer [String] Int. Now I can run it:

    ghci> runWriter multWithLog
    (15, ["Got number: 3","Got number: 5"])
    

    And you see that we log all of the intermediate operations.

    Why is the code written like this?

    Why bother to create the MonadWriter type class at all? The reason is to do with monad transformers. As you correctly realised, the simplest way to implement Writer is as a newtype wrapper on top of a pair:

    newtype Writer w a = Writer { runWriter :: (a,w) }
    

    You can declare a monad instance for this, and then write the function

    tell :: Monoid w => w -> Writer w ()
    

    which simply logs its input. Now suppose you want a monad that has logging capabilities, but also does something else – say it can read from an environment too. You’d implement this as

    type RW r w a = ReaderT r (Writer w a)
    

    Now because the writer is inside the ReaderT monad transformer, if you want to log output you can’t use tell w (because that only operates with unwrapped writers) but you have to use lift $ tell w, which “lifts” the tell function through the ReaderT so that it can access the inner writer monad. If you wanted two layers transformers (say you wanted to add error handling as well) then you’d need to use lift $ lift $ tell w. This quickly gets unwieldy.

    Instead, by defining a type class we can make any monad transformer wrapper around a writer into an instance of writer itself. For example,

    instance (Monoid w, MonadWriter w m) => MonadWriter w (ReaderT r m)
    

    that is, if w is a monoid, and m is a MonadWriter w, then ReaderT r m is also a MonadWriter w. This means that we can use the tell function directly on the transformed monad, without having to bother with explicitly lifting it through the monad transformer.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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 want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.