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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:58:35+00:00 2026-06-14T15:58:35+00:00

I have a pretty-printer like that: somefun = text woo $+$ nest 4 (text

  • 0

I have a pretty-printer like that:

somefun = text "woo" $+$ nest 4 (text "nested text") $+$ text "text without indent"
fun = text "------" $+$ somefun

What I want from it is to print this:

------ woo
    nested text
text without indent

But it prints:

------
woo
    nested text
text without indent

I can understand why it prints like this, but I’m having trouble to do what I want. One solution I find was this:

somefun p = p <+> text "woo" $+$ nest 4 (text "nested text") $+$ text "text without indent"
fun = somefun (text "------")

That is, I’m passing the Doc which I want my next Doc’s indentation to be based on. This solves my problem but I’m looking for better ways to do this.

  • 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-14T15:58:36+00:00Added an answer on June 14, 2026 at 3:58 pm

    Your pass-the-Doc-as-an-argument solution is good. Once you’ve combined into a single Doc, you can’t split it apart again, so here are two ways that use lists instead:

    Alternative 1

    Another way of doing this is to use [Doc] instead of Doc for your subsequent text, if you want to treat the lines differently, then recombine using something like

    (<+$) :: Doc -> [Doc] -> Doc
    doc <+$ [] = doc 
    doc <+$ (d:ds) = (doc <+> d) $+$ foldr ($+$) empty ds
    
    somefun :: [Doc]
    somefun = [text "woo",
        nest 4 (text "nested text"),
        text "text without indent"]
    
    fun :: Doc
    fun = text "------" <+$ somefun
    

    This gives you

    *Main> fun
    ------ woo
        nested text
    text without indent
    

    Alternative 2

    You could rewrite this solution another way keeping lists, if you like to keep indenting the top line:

    (<+:) :: Doc -> [Doc] -> [Doc]
    doc <+: [] = [doc] 
    doc <+: (d:ds) = (doc <+> d) : ds -- pop doc in front.
    

    We’ll need to put those together into a single Doc at some stage:

    vsep = foldr ($+$) empty
    

    Now you can use : to put a line above, and <+: to push a bit in front of the top line:

    start = [text "text without indent"]
    next  = nest 4 (text "nested text") : start
    more  = text "woo" : next
    fun   = text "------" <+: more
    extra = text "-- extra! --" <+: fun
    

    Test this with

    *Main> vsep fun
    ------ woo
        nested text
    text without indent
    
    *Main> vsep extra
    -- extra! -- ------ woo
        nested text
    text without indent
    

    The main issue is that if you use [Doc] instead of Doc it’s almost as if you’re not using the pretty-print library! It doesn’t matter, though, if it’s what you need.

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

Sidebar

Related Questions

basically i want to hold a parameter that retrieve value from $.post() call like
I have a pretty large object that I need to pass to a function
I have written a pretty-printer using the GDB Python interface for a (C) struct
I'm writing a pretty-printer for a simple white-space sensitive language. I like the Leijen
I have an object that consists primarily of a very large nested dictionary: class
I have an html page that is printed pretty well thanks to print specific
I feel like I have a pretty good handle on pointers in C. Mostly,
I would like to be able to have a Printer installed on a windows
I have a text editing program that saves it's output into a PDF file.
I have a pretty long and wide datagrid which I would like to send

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.