lambda抽象ADT的显示实例

dim*_*im8 1 haskell instance show typeclass

所以,我已经定义了lambda数据类型:

data LExpr
    = Variable String         -- variable
    | Apply LExpr LExpr       -- function application
    | Lambda String LExpr     -- Lambda abstraction 
    deriving (Eq, Show)  
Run Code Online (Sandbox Code Playgroud)

现在我想实现Show自己的一个实例.我已经完成了show'大部分工作的功能,但没有使用实例:

 show' :: LExpr -> String
 show' (Variable a) = a
 show' (Apply e1 e2) = "(" ++ show' e1 ++ " " ++ show' e2 ++ ")"                   
 show' (Lambda x e) = "(? " ++ x ++ ". " ++ show' e ++ ")"    
Run Code Online (Sandbox Code Playgroud)

我如何实现它,以获得以下输出而不使用显式show'函数:

Main> (Apply (Lambda "x" (Apply (Variable "x") (Variable "y"))) (Variable "z"))
((? x. x y) y)       
Run Code Online (Sandbox Code Playgroud)

Ale*_*lec 5

Show类添加实例声明.

instance Show LExpr where
  show = show'
Run Code Online (Sandbox Code Playgroud)

并删除该deriving(Show)部分

data LExpr
    = Variable String         -- variable
    | Apply LExpr LExpr       -- function application
    | Lambda String LExpr     -- Lambda abstraction 
    deriving (Eq) 
Run Code Online (Sandbox Code Playgroud)