[Haskell-cafe] foild function for expressions

David Menendez dave at zednenem.com
Tue Dec 4 13:50:38 EST 2007


On Dec 3, 2007 12:18 PM, Carlo Vivari <thrakatak at hotmail.com> wrote:

>
> 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) = ¿¿¿???
>

You'll want something like this:

foldExp alg (Add e1 e2) = add alg (foldExp alg e1) (foldExp alg e2)


-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071204/c2f858d3/attachment.htm


More information about the Haskell-Cafe mailing list