Created
September 20, 2012 00:36
-
-
Save linstantnoodles/3753246 to your computer and use it in GitHub Desktop.
ocaml p6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* Write a function val zip : 'a list * 'b list -> 'a * 'b list which accepts a | |
* pair of lists of the same length and returns a list of pairs. For example, | |
* zip([1; 2; 3], ['A'; 'B'; 'C']) should return the list of pairs [(1, 'A'); (2, | |
* 'B'); (3, 'C')]. You may assume that the 2 input lists are of the same length. | |
* (2 Points)*) | |
let rec zip (a,b) = | |
match (a,b) with | |
([],[]) -> [] | |
| ([],n::ns)-> [] | |
| (n::ns,[]) -> [] | |
| (k::ks, h::hs) -> (k,h)::zip(ks,hs);; | |
(* Write a function val unZip : 'a * 'b list -> 'a list * 'b list | |
* which accepts a list of pairs and returns a pair of lists. For example, unZip | |
* [(1, 'A'); (2, 'B'), (3, 'C')] should return the pair of lists ([1; 2; 3], | |
* ['A'; 'B'; 'C'])*) | |
let ua a = match a with (a,b)->a;; | |
let ub a = match a with (a,b)->b;; | |
let rec unzip list = | |
match list with | |
[] -> ([],[]) | |
| (a,b)::ns -> (a::ua(unzip(ns)),b::ub(unzip(ns)));; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment