mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
5a ex11
This commit is contained in:
+9
-2
@@ -271,10 +271,17 @@ data Nat = Zero | PlusOne Nat
|
|||||||
deriving (Show,Eq)
|
deriving (Show,Eq)
|
||||||
|
|
||||||
fromNat :: Nat -> Int
|
fromNat :: Nat -> Int
|
||||||
fromNat n = todo
|
fromNat n = case n of
|
||||||
|
PlusOne n -> 1 + fromNat n
|
||||||
|
Zero -> 0
|
||||||
|
|
||||||
toNat :: Int -> Maybe Nat
|
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
|
-- Ex 12: While pleasingly simple in its definition, the Nat datatype is not
|
||||||
|
|||||||
Reference in New Issue
Block a user