Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #############################################
- // Hand-in 02.3 – List functions
- let rec pclElementAt ls n =
- match n, ls with
- | _, [] -> 0
- | 1, (h::tl) -> h
- | _, (h::tl) -> pclElementAt tl (n-1)
- //> pclElementAt [2; 4; 6; 8; 10] 2;;
- //val it : int = 4
- // #############################################
- // Hand-in 02.4 – List functions
- // a.
- let rec pclReverse ls =
- match ls with
- | [] -> []
- | (h::tl) -> pclReverse tl @ [h]
- //> pclReverse [2; 4; 6; 8; 10];;
- //val it : int list = [10; 8; 6; 4; 2]
- // b.
- let isPalindrome ls =
- if ls = pclReverse ls then true
- else false
- //> isPalindrome [1;2;3;2;2];;
- //val it : bool = false
- //> isPalindrome [1;2;3;2;1];;
- //val it : bool = true
- // #############################################
- // Hand-in 02.5 – List functions
- let rec pclZipper a b =
- match a, b with
- | [], [] -> []
- | (aH::aTl), (bH::bTl) -> if (a.Length = b.Length) then (aH, bH) :: pclZipper aTl bTl else failwith "Lists are different length"
- //> pclZipper [1;2;3;4] ['a';'b';'c';'d'];;
- //val it : (int * char) list = [(1, 'a'); (2, 'b'); (3, 'c'); (4, 'd')]
- // if length is not the same, using Options
- let rec pclZipperDiffLength a b =
- match a, b with
- | [], [] -> []
- | _, [] -> (Some a.Head, None) :: pclZipperDiffLength a.Tail []
- | [], _ -> (None, Some b.Head) :: pclZipperDiffLength [] b.Tail
- | (aH::aTl), (bH::bTl) -> (Some aH, Some bH) :: pclZipperDiffLength aTl bTl
- //> pclZipperDiffLength [1;2;3;4] ['a';'b';'c';'d';'e';'f'];;
- //val it : (int option * char option) list =
- // [(Some 1, Some 'a'); (Some 2, Some 'b'); (Some 3, Some 'c');
- // (Some 4, Some 'd'); (null, Some 'e'); (null, Some 'f')]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement