[Haskell-cafe] foild function for expressions

Stefan O'Rear stefanor at cox.net
Mon Dec 3 22:13:08 EST 2007


On Mon, Dec 03, 2007 at 09:18:18AM -0800, Carlo Vivari 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) = ¿¿¿???
> 
> ..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)

The problem is that AlgExp defines an arbitrary algebra, but in order to
fold you need a universal algebra.  So it makes the most sense to add
foldExp to the reccord.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071203/582cffe3/attachment.bin


More information about the Haskell-Cafe mailing list