(*** Exercice 1 ***) succ :: Int -> Int succ x = x+1 pred :: Int -> Int pred x = if (x<=0) then 0 else (x-1) is_zero :: Int -> Bool is_zero x = (x == 0) empty = [] -- the empty list cons x e = (x:e) -- cons of a head and a tail is_empty l = (l == []) -- tests if a list is empty or not (*** Exercice 2 ***) data List a = Nil | Cons a (List a) instance Show a => Show (List a) where show Nil = "[]" show (Cons x l) = (show x) ++ ";" ++ (show l) (*** Exercice 3 ***) data JValue = JChar Char | JNumber Double | JArray [JValue] deriving (Eq, Ord, Show) example1 :: [Double] example1 = [1,2,3] main = do putStrLn $ show $ JArray (map JNumber example1) class JSON a where toJValue :: a -> JValue fromJValue :: JValue -> Maybe a instance JSON JValue where toJValue = id fromJValue = Just unsafefromJValue :: JValue -> a example2 :: JValue example2 = JArray ([JNumber 2.0, JNumber 3.0]) example3 :: JValue example3 = JArray ([ JChar 'a', JNumber 3.0]) (unsafefromJValue example2)::([Double]) (fromJValue example3) ::(Maybe [Double]) (unsafefromJValue example3)::([Double]) (*** Exercice 4 ***) let l = ref [];; (* val l : '_a list ref *) l := 1::(!l);; l;; (* val l : int list ref *) let f = fst (fst, fst);; (* val f : '_a * '_b -> '_a *) f (1,'a');; f ;; (* val f : int * char -> int *) arg_show x = show x -- allowed no_arg_show = show -- not allowed