« Return to Thread: foild function for expressions

foild function for expressions

by Carlo Vivari :: Rate this Message:

Reply to Author | View in Thread

Hi! I'm a begginer in haskell and I have a problem with an exercise, I expect someone could help me:

In one hand I have a declaration of an algebra data, like this:

data AlgExp a = AlgExp
{ litI  :: Int -> a,
   litB :: Bool -> a,
   add :: a -> a -> a,
   and :: a -> a -> a,
   ifte :: a -> a -> a -> a}

(being ifte an 'ifthenelse' expresion...)

What I want to do is to write a fold function for expressions, something like this:

foldExp :: AlgExp a -> Exp -> a
foldExp alg (LitI i) = litI alg i
foldExp alg (LitB i) = litB alg i
foldExp alg (add exp1 exp2) = ¿¿¿???
foldExp alg (and exp1 exp2) = ¿¿¿???
foldExp alg (ifte exp1 exp2 exp3) = ¿¿¿???

..ETC


the fact is that I have no idea of what to do with the other expresions (add, and, and ifte)... I really don' t understand how to do this... It's clear that a fold function should colapse in one valour, but how can I espress it in the terms of the exercise?

For further information about the problem after this,  it's suposed that I have to rewrite some functions for expresions but in terms of foldexp (the one I should write before)


Thank you very much!!!!

 « Return to Thread: foild function for expressions