(* ---------- ex12 ---------- *) let f1 x = x + 1 ;; f1 2 ;; let f2 x = f1 x ;; f2 2 ;; let f3 x = 1 + x 5 ;; f3 (fun y -> y + 1) ;; let f4 x = x ;; f4 5 ;; (* ---------- ex13 ---------- *) let rec succAll l = match l with | [] -> [] | el::xl -> (el+1) :: succAll xl ;; succAll [] ;; succAll [3; 6; 1; 0; -4] ;; (* ---------- ex14 ---------- *) let rec belongs n l = match l with | [] -> false | el::xl -> if (el = n) then true else belongs n xl ;; belongs 4 [1;2;3;4;5;6] ;; belongs 4 [1;2] ;; let rec union l1 l2 = match l1 with | [] -> l2 | el::xl1 -> if (belongs el l2) then union xl1 l2 else el::union xl1 l2 ;; union [7;3;9] [2;1;9] ;; let rec inter l1 l2 = match l1 with | [] -> [] | el::xl1 -> if (belongs el l2) then el::inter xl1 l2 else inter xl1 l2 ;; inter [7;3;9] [2;1;9] ;; let rec diff l1 l2 = match l1 with | [] -> [] | el::xl1 -> if (belongs el l2) then diff xl1 l2 else el::diff xl1 l2 ;; diff [7;3;9] [2;1;9] ;; let rec ins v l = match l with | [] -> [] | x::xl -> (v::x) :: ins v xl ;; let rec power l = match l with | [] -> [[]] | el::xl -> power xl @ ins el (power xl) ;; power [] ;; power [2;3] ;; power [1;2;3] ;; (* ---------- ex15 ---------- *) let rec nat n = if (n=0) then [] else (n-1) :: nat (n-1) ;; nat 0 ;; nat 1 ;; nat 2 ;; nat 5 ;; (* ---------- ex16 ---------- *) let equal n l = match l with | [] -> false | el::l -> if (el = n) then true else false ;; equal 1 [1; 0] ;; equal 0 [0; 1] ;; equal 0 [1; 0] ;; let rec count n l = match l with | [] -> 0 | el::xl -> if (el = n) then 1 + count n xl else count n [] ;; count 10.1 [10.1; 10.1; 10.1; 10.1; 10.1; 10.1; 10.1; 10.0; 10.0; 10.1; 10.0] ;; count 10.0 [10.1; 10.1; 10.1; 10.1; 10.1; 10.1; 10.1; 10.0; 10.0; 10.1; 10.0] ;; count 10.0 [10.0; 10.0; 10.1; 10.0] ;; let rec remove n l = match l with | [] -> [] | (el,_)::xl -> if (n = el) then remove n xl else l ;; remove 10.1 [(10.1, 7); (10., 2); (10.1, 1); (10., 1)] ;; let rec pack l = match l with | [] -> [] | el::xl -> (el, count el l)::remove el (pack xl) ;; pack [] ;; pack [10.1; 10.1; 10.1; 10.1; 10.1; 10.1; 10.1; 10.0; 10.0; 10.1; 10.0] ;; let rec unpackaux n el = if (n = 0) then [] else (el)::(unpackaux (n-1) el) ;; unpackaux 7 10.1 ;; unpackaux 2 10.0 ;; unpackaux 1 10.1 ;; let rec unpack l = match l with | [] -> [] | (el,n)::xl -> (unpackaux n el)::unpack xl ;; unpack [] ;; unpack [(10.1, 7); (10.0, 2); (10.1, 1); (10.0,1)] ;;