1 Replies - 582 Views - Last Post: 29 January 2017 - 10:58 AM

#1 NoYouDidnt  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 55
  • Joined: 04-July 15

Pretty Printer In Haskell

Posted 29 January 2017 - 10:09 AM

Have this type:

data Expr = TRUE | FALSE | Zero | Succ Expr | Pred Expr | IsZero Expr | If Expr Expr Expr
    deriving Show


Trying to write a function pp2html :: Int -> Expr -> String

so that if I have

term = Pred (If (IsZero Zero) (Succ (Succ Zero)) Zero)

html = do
  putStr "<html>\n  <body>\n    <ul>\n"
  putStr (pp2html 6 term)
  putStr "    <ul>\n  </body>\n</html>\n"



The program should print

<html>
  <body>
    <ul>
      <li>pred
        <ul>
          <li>if
            <ul>
              <li>iszero
                <ul>
                  <li>zero</li>
                </ul>
              </li>
            </ul>
            <ul>
              <li>succ
                <ul>
                  <li>succ
                    <ul>
                      <li>zero</li>
                    </ul>
                  </li>
                </ul>
              </li>
            </ul>
            <ul>
              <li>zero</li>
            </ul>
          </li>
        </ul>
      </li>
    <ul>
  </body>
</html>



I am really stuck at this problem. I don't know how to approach it and need some advice.

Is there a recursive function call for every left parentheses, in that case how do I pick the belonging right parentheses? Do I need to tokenize the string somehow? Any help is appreciated! thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Pretty Printer In Haskell

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2506
  • View blog
  • Posts: 3,959
  • Joined: 21-June 11

Re: Pretty Printer In Haskell

Posted 29 January 2017 - 10:58 AM

There's a recursive call for every sub-node of your tree - parentheses don't matter.

It looks like the logic you want for compound expressions is: "<ul><li>foo</li>" where foo is the name of the current node and then "<li>" ++ pp2html subexpr ++ "</li>" for each of the current expression's subexpressions and finally "</ul>". And for atomic expressions you just want the name of the expression without "<ul>" or anything. At least that's what I gathered from your example output, I'm sure this is spelled out more precisely in your problem statement.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1