“如何按层次划分”;巢;Haskell pretty打印缩进
我想用Haskell Pretty软件包打印一个AST 这一切都很好,但嵌套的构造不能正确缩进 我是这样做的:“如何按层次划分”;巢;Haskell pretty打印缩进,haskell,pretty-print,Haskell,Pretty Print,我想用Haskell Pretty软件包打印一个AST 这一切都很好,但嵌套的构造不能正确缩进 我是这样做的: draw :: Pretty a => a -> String draw = render.pretty pretty (Letin d c ) = text "let" <+> text (draw d) $$ nest 4 (text "in" <+> text (draw c)) le
draw :: Pretty a => a -> String
draw = render.pretty
pretty (Letin d c ) = text "let" <+> text (draw d) $$
nest 4 (text "in" <+> text (draw c))
let Const x := 2
in let Var y := Int
in y = 3; let Var z := Int
in z = 0; z = z + 1
似乎嵌套级别不是继承的,因此所有嵌套级别都在+4边距上是绝对的,而不是在每个级别上连续缩进,即相对于当前缩进级别的父级+4。您的意思是递归调用
pretty
?从你的问题我看不出来
一个快速测试,尝试重现您所做的工作:
import Text.PrettyPrint
data Letin = Letin String (Maybe Letin)
draw = show
pretty (Letin d c ) =
text "let" <+> text (draw d) $$
nest 4 (text "in" <+> case c of Nothing -> text "empty";
Just c' -> pretty c')
因此,您可能需要列出更多代码。是的,递归地。我遗漏了绘画的定义;上面加的,我想应该和你的一样。我的错误是在使用render.pretty的“text(draw e)”上递归,而不是直接在“pretty e”上递归。虽然我不知道为什么它坏了。。。谢谢
let "x"
in let "y"
in empty