(*** Exercice 1 ***) succ x = x+1 pred x = if (x<=0) then 0 else (x-1) 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 -- The take2 function takes a lot of arguments and returns the second. -- take2 :: t -> t1 -> t2 -> t3 -> t4 -> t1 take2 = \a z e r t -> z -- The fixpoint function takes a function f and a starting point and, if -- it returns, returns a fixpoint of f. -- fixpoint :: (Eq a) => (a -> a) -> a -> a fixpoint f xs = if ((f xs) == xs) then xs else fixpoint f (f xs) -- Test of the fixpoint function : -- fixpoint (\x -> if (x <= 5) then x + 1 else x) 0 -- returns 6 -- The search function looks up for an element inside a list, and -- returns the element or Nothing. -- search :: (Eq a) => a -> [a] -> Maybe a search el l = if (is_empty l) then Nothing else if (head l == el) then Just (head l) else search el (tail l) -- The generalized search abstracts the previous function over the list type. -- search_gen :: (Eq a) => -- (t -> Bool) -> (t -> a) -> (t -> t) -> a -> t -> Maybe a search_gen is_empty head tail el l = if (is_empty l) then Nothing else if (el == head l) then Just el else search_gen is_empty head tail el (tail l) -- The fold function is a generic folding algorithm on lists. -- fold :: (t1 -> t -> t1) -> t1 -> [t] -> t1 fold f x l = if (is_empty l) then x else fold f (f x (head l)) (tail l) -- The search function defined in terms of the fold search_fold el l = fold (\x y -> if (x == Nothing) && (y == el) then Just y else x) Nothing l (*** Exercice 2 ***) import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.Arrays; class SortableProgram { interface Sortable> { void sort(); } static class SortableLinkedList> extends LinkedList implements Sortable { public SortableLinkedList(Collection c) { super(c); } public void sort() { Collections.sort(this, (o1,o2) -> o1.compareTo(o2)); } } public static void main(String[] args) { SortableLinkedList l = new SortableLinkedList( Arrays.asList(7, 2, 4, 0, 3)); System.out.println(l.toString()); l.sort(); System.out.println(l.toString()); } } (*** Exercice 3 ***) data List a = Nil | Cons a (List a) instance Show a => Show (List a) where show Nil = "[]" show l = "[" ++ (show_rec l) ++ "]" where show_rec (Cons x Nil) = (show x) show_rec (Cons x l) = (show x) ++ ";" ++ (show_rec l) (*** Exercice 4 ***) data JValue = JString String | JNumber Double | JArray [JValue] deriving (Eq, Ord, Show) class JSON a where toJValue :: a -> JValue fromJValue :: JValue -> Maybe a instance JSON JValue where toJValue = id fromJValue = Just instance JSON Double where toJValue = JNumber fromJValue (JNumber d) = Just d fromJValue _ = Nothing instance (JSON a) => JSON [a] where toJValue = JArray . map toJValue fromJValue (JArray l) = unval (map fromJValue l) [] where unval [] l = Just $ reverse l unval (x:xs) l = case x of Nothing -> Nothing Just t -> unval xs (t:l) fromJValue _ = Nothing class (JSON a) => JSON_Ext a where unsafefromJValue :: JValue -> a instance JSON_Ext JValue where unsafefromJValue = id instance JSON_Ext Double where unsafefromJValue (JNumber d) = d instance JSON_Ext Char where unsafefromJValue (JChar d) = d instance JSON_Ext a => JSON_Ext [a] where unsafefromJValue (JArray l) = case fromJValue (JArray l) of Just j -> j example1 :: [Double] example1 = [1,2,3] example2 :: JValue example2 = JArray ([JNumber 2.0, JNumber 3.0]) example3 :: JValue example3 = JArray ([ JChar 'a', JNumber 3.0]) main = do putStrLn $ show $ toJValue example1 -- putStrLn $ show $ fromJValue $ toJValue example -- does not type putStrLn $ show $ ((fromJValue $ toJValue example1) :: Maybe [Double]) putStrLn $ show $ ((unsafefromJValue example2) ::([Double])) putStrLn $ show $ ((fromJValue example3) ::(Maybe [Double])) putStrLn $ show $ ((unsafefromJValue example3)::([Double])) -- runtime error