Jung
Jung

Reputation: 139

Understanding the eval function used to define combinators and expression (Lambda) in Haskell

I find the use of pattern matching for eval (App x y) redundant since both cases would return App x y. I wonder if eval (App x y) is needed at all because we have eval x = x at the end, which should also include eval (App x y)

data Expr = App Expr Expr | S | K | I | Var String | Lam String Expr deriving (Show,Eq)
eval :: Exp -> Exp
eval (App I x) = eval x 
eval (App (App K x) y) = eval x
eval (App (App (App S f) g) x) = eval (App (App f x) (App g x))
eval (App x y)
 | evalx == x  = (App evalx (eval y)) --test if x is a Lam (not other possible values of Exp)
 | otherwise = eval (App evalx y)
     where evalx = eval x
eval (Var x) = (Var x)
eval x = x  

Upvotes: 1

Views: 97

Answers (0)

Related Questions