[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-beginners
Subject:    Re: [Haskell-beginners] foldr for Nats
From:       Daniel Fischer <daniel.is.fischer () web ! de>
Date:       2010-01-26 11:23:06
Message-ID: 201001261223.06919.daniel.is.fischer () web ! de
[Download RAW message or body]

Am Dienstag 26 Januar 2010 11:52:45 schrieb kane96@gmx.de:
> Hello,
> I have:
> data Nat = Z | S Nat deriving (Eq,Ord,Show)
> and should write a function that works for Nats like foldr for list
> where: instance Enum Nat where
>     toEnum i | i < 0 = error "foo"
>
>                     | i == 0 = Z
>                     | otherwise = S (toEnum (i-1))
>
> Can someone give me a hint how to do this?

I don't understand what "works for Nats like foldr for lists" means.
A very nice explanation of how foldr works is

foldr f z
   (a1  :  (a2  :  (a3  :  (...  :  (an  :  []) ...))))
~> (a1 `f` (a2 `f` (a3 `f` (... `f` (an `f` z) ...))))

So we replace the constructor [] with the base value z, and the constructor 
(:) with the function f.

Maybe it's meant to be similar, foldNat f z should replace the constructor 
Z with the base value and the constructor S with the function.
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic