Commit 054c1a97 authored by Taddeüs Kroes's avatar Taddeüs Kroes

funclang series3: Added assignment 7.

parent e37f59f9
open List
(* 1 *)
let rec nth l n =
match n with
| 0 -> hd l
| i when i > 0 && i < (length l) -> nth (tl l) (i - 1)
| _ -> raise (Failure "list index out of bounds")
;;
(* 2 *)
let heads l = map hd l;;
(* 3 *)
let zip l m =
let rec cut l e =
let len = (length l) in
if e = len then l else cut (rev (tl (rev l))) e
in
let min a b = if a > b then b else a in
let len = min (length l) (length m) in
List.combine (cut l len) (cut m len)
;;
(* 4 *)
let rec map f l = if length l = 0 then [] else (f (hd l))::(map f (tl l));;
(* 5 *)
let rec reduce f z l =
match l with
| [] -> z
| h::t -> f z (reduce f h t)
;;
(* 6 *)
let rec isSublist l s =
match s with
| [] -> true
| h::t -> h = (hd l) && isSublist (tl l) t
;;
(* 7 *)
let rec lookup l key =
match l with
| [] -> raise (Failure "lookup")
| h::t -> let k, v = h in if k = key then v else lookup t key
;;
(* Challenge *)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment