Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

Codebeispiel: eval

Vereinfachung des Codes mit Applikativen Funktor

Haskell source code icon Eval.hs — Haskell source code, 1 KB (1582 bytes)

Dateiinhalt

import Data.Maybe
import Data.Functor
import Control.Applicative

data Exp v = Var v
           | Val Int
           | Neg (Exp v)
           | Add (Exp v ) (Exp v )
           | Mul (Exp v ) (Exp v )
  deriving (Eq)

-- Lambda Terme hübsch anzeigen:
instance (Show a) => Show (Exp a) where
  showsPrec _ (Var v)   = shows v
  showsPrec _ (Val x)   = shows x
  showsPrec n (Neg p)   = showString "-" . showsPrec 2 p
  showsPrec n (Add p q) = showParen (n>1) $ (showsPrec 1 p) . showString " + " . showsPrec 1 q
  showsPrec n (Mul p q) = showParen (n>0) $ (showsPrec 2 p) . showString " * " . showsPrec 2 q

type Var = String
newtype Env = Env [(Var,Int)]  
  deriving Show

fetch :: Var -> Env -> Int
fetch v (Env env) = fromJust $ lookup v env

env1 = Env [("x",1),("y",2),("z",3)]
env2 = Env [("x",1),("y",1),("z",1)]
env3 = Env [("x",0),("y",10),("z",25)]

expX = Var "x"
expY = Var "y"
expZ = Var "z"

exp1 = Add expX expY
exp2 = Add expX $ Neg expY
exp3 = Mul (Add expX $ Neg expY) (Add expX expZ)
exp4 = Add (Val 1) (Mul (Neg exp3) exp2)

-- Ohne Funktor:

eval :: Exp String -> Env -> Int
eval (Var x )  env = fetch x env
eval (Val i )  env = i
eval (Neg p )  env = negate $ eval p env 
eval (Add p q) env = eval p env + eval q env
eval (Mul p q) env = eval p env * eval q env

-- Mit Funktor ((->) r):

eval' :: Exp String -> Env -> Int
eval' (Var x)   = fetch x
eval' (Val i)   = pure i
-- eval' (Neg p)   = (pure negate) <*> eval' p
eval' (Neg p)   = negate <$> eval' p
eval' (Add p q) = pure (+) <*> eval' p <*> eval' q
eval' (Mul p q) = (*) <$>  eval' p <*> eval' q 


Artikelaktionen


Funktionsleiste