This commit is contained in:
Aaro Koivukangas
2025-12-23 01:27:46 +02:00
parent 501d13ddb3
commit df26a99410
+9 -2
View File
@@ -271,10 +271,17 @@ data Nat = Zero | PlusOne Nat
deriving (Show,Eq)
fromNat :: Nat -> Int
fromNat n = todo
fromNat n = case n of
PlusOne n -> 1 + fromNat n
Zero -> 0
toNat :: Int -> Maybe Nat
toNat z = todo
toNat z = if z >= 0 then Just (toNat' z) else Nothing
where
toNat' :: Int -> Nat
toNat' z
| z>0 = PlusOne $ toNat' (z-1)
| z==0 = Zero
------------------------------------------------------------------------------
-- Ex 12: While pleasingly simple in its definition, the Nat datatype is not