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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:08:14+00:00 2026-05-28T04:08:14+00:00

I’m relatively new to Haskell. I’m creating a small api/dsl on top of happstack-lite

  • 0

I’m relatively new to Haskell. I’m creating a small api/dsl on top of happstack-lite, which will have an interface more similar to Sinatra, mostly to learn. As a part of this, I want to construct an array using do syntax (basically because it would be prettier than the msum [route, route, route] thing. The usage of the monad would look something like this:

someFunctionThatMakesStrings :: String

unwrapMyMonadAndGiveMeAList :: MyMonad _ -> [String]

makeAList :: [String]
makeAList = unwrapMyMonadAndGiveMeAList do
    someFunctionThatMakesStrings
    someFunctionThatMakesStrings
    someFunctionThatMakesStrings
    ...

So makeAList would return a list with 3 strings. Notice that I would like to use functions inside it that aren’t aware of the monad (they just return a string).

I can do this with Writer, but each function called has to be aware of the Writer monad, and it also seems like overkill (I don’t need the return type tuple, and the way I got it to work involved lots of wrapping/unwrapping)

I tried using the list monad itself, but it clearly is intended for something different than this.

So which of my assumptions need to change, and then how would I make a new list-construction monad from scratch? How close can I get?

  • 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-28T04:08:15+00:00Added an answer on May 28, 2026 at 4:08 am

    Writer is definitely what you want here; you can avoid “exposing” Writer to the outside at the cost of a little more overhead in the definitions themselves:

    foo :: [String]
    foo = execWriter $ do
      tell otherList1
      tell otherList2
      tell otherList3
    
    otherList1 :: [String]
    otherList1 = execWriter $ do
      ...
    

    i.e., you can keep the use of Writer local to each definition, but you have to wrap each list you want to use as a “source” in tell. The key thing here is to use execWriter, which discards the result element of the tuple (it’s identical to snd . runWriter).

    However, if you have a lot of definitions like this, I would recommend simply using Writer directly, and only applying execWriter in the place where you want the combined result; you can make the types a bit cleaner by defining a synonym like type Foo = Writer [String].

    I’m not sure what advantage constructing your own list-creation monad would be; it would end up being identical to Writer [String].

    The list monad is indeed irrelevant to what you want to do here.


    As far as defining your own list-writing monad goes, it’s pretty simple:

    data ListWriter a = ListWriter a [String]
    
    runListWriter :: ListWriter a -> (a, [String])
    runListWriter (ListWriter a xs) = (a, xs)
    
    execListWriter :: ListWriter a -> [String]
    execListWriter = snd . runListWriter
    
    instance Monad ListWriter where
      return a = ListWriter a []
      ListWriter a xs >>= f = ListWriter b (xs ++ ys)
        where ListWriter b ys = f a
    

    The only tricky part is (>>=), where we have to take only the value part of the left argument, feed it into the right hand argument, take it apart, and then combine the two lists inside, wrapping it back up with the result of the right hand side.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.