Commit 194bbca3 authored by Taddeus Kroes's avatar Taddeus Kroes

funclang series3: made challenge code more compact and fixed isSublist.

parent 1229ac92
...@@ -2,6 +2,7 @@ open List ...@@ -2,6 +2,7 @@ open List
(* 1 *) (* 1 *)
let rec nth l n = let rec nth l n =
(* *)
match n with match n with
| 0 -> hd l | 0 -> hd l
| i when i > 0 && i < (length l) -> nth (tl l) (i - 1) | i when i > 0 && i < (length l) -> nth (tl l) (i - 1)
...@@ -13,14 +14,20 @@ let heads l = map hd l;; ...@@ -13,14 +14,20 @@ let heads l = map hd l;;
(* 3 *) (* 3 *)
let zip l m = let zip l m =
(* Cut off a list l at length e *)
let rec cut l e = if e = (length l) then l else cut (rev (tl (rev l))) e in let rec cut l e = if e = (length l) then l else cut (rev (tl (rev l))) e in
(* Get the minimum of two integers *)
let min a b = if a > b then b else a in let min a b = if a > b then b else a in
(* Get the length of the shortest list *)
let len = min (length l) (length m) in let len = min (length l) (length m) in
(* Cut off both lists at the length of the shortest list to equal their
* lengts, then use the native 'combine' function *)
combine (cut l len) (cut m len) combine (cut l len) (cut m len)
;; ;;
(* 4 *) (* 4 *)
let rec map f l = let rec map f l =
(* Apply f to the head, then map it to the tail recursively *)
match l with match l with
| [] -> [] | [] -> []
| h::t -> f h::map f t | h::t -> f h::map f t
...@@ -28,6 +35,7 @@ let rec map f l = ...@@ -28,6 +35,7 @@ let rec map f l =
(* 5 *) (* 5 *)
let rec reduce f z l = let rec reduce f z l =
(* Apply f to the head, and the reducement of the tail *)
match l with match l with
| [] -> z | [] -> z
| h::t -> f z (reduce f h t) | h::t -> f z (reduce f h t)
...@@ -35,13 +43,21 @@ let rec reduce f z l = ...@@ -35,13 +43,21 @@ let rec reduce f z l =
(* 6 *) (* 6 *)
let rec isSublist l s = let rec isSublist l s =
match s with (* Check if l starts with s, otherwise chech the tail recursively *)
| [] -> true let rec startsWith l s =
| h::t -> h = (hd l) && isSublist (tl l) t (length s) <= (length l) && match s with
| [] -> true
| h::t -> h = (hd l) && startsWith (tl l) t
in
match l with
| [] -> false
| _::t -> (startsWith l s) || (isSublist t s)
;; ;;
(* 7 *) (* 7 *)
let rec lookup l key = let rec lookup l key =
(* Math key to the first tuple and return the value-side of the tuple if
* matched. Otherwise, lookup in the rest of the tuples *)
match l with match l with
| [] -> raise (Failure "lookup") | [] -> raise (Failure "lookup")
| h::t -> let k, v = h in if k = key then v else lookup t key | h::t -> let k, v = h in if k = key then v else lookup t key
...@@ -49,13 +65,11 @@ let rec lookup l key = ...@@ -49,13 +65,11 @@ let rec lookup l key =
(* Challenge *) (* Challenge *)
let rec coin_permutation l n = let rec coin_permutation l n =
let perms coin l = (* Reduce the list using the fact that the the total number of coin
match coin with * permutations is equal to the number of permutations given a single coin,
| i when i < n -> 1 + (coin_permutation l (n - coin)) * added to the number of permutations without that coin *)
| i when i = n -> 1
| _ -> 0
in
match l with match l with
| [] -> 0 | [] -> 0
| h::t -> (perms h t) + (coin_permutation t n) | h::t -> (if h > n then 0 else 1 + (coin_permutation t (n - h)))
+ (coin_permutation t n)
;; ;;
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