week1 code1

week1 code1 - |(h:t> let(a,b = split l in let a_sorted =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
http://www.cs.ucsd.edu/classes/fa06/cse130/lectures/s1.ml let rec split l = match l with [] -> ([],[]) | [h] -> ([h],[]) | (h::h2::t) -> let (a,b) = split t in (h::a,h2::b) (*[1;5;6] [2;3;4] -> [1;2;3;4;5;6]*) let rec merge (a,b) = match (a,b) with ([],b) -> b | (a,[]) -> a | (h1::t1,h2::t2) -> if h1>h2 then h2::merge (a,t2) else h1::merge (t1,b) let rec mergesort l = match l with [] -> [] | [x] -> [x]
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: | (h::t) -> let (a,b) = split l in let a_sorted = mergesort a in let b_sorted = mergesort b in merge (a_sorted,b_sorted) (* [[1;2;3];[5;6];[4;1]] -> [1;2;3;5;6;4;1] *) let rec flatten l = match l with -> | [x] -> x | (h::t) -> h@(flatten t);; flatten [[1;2;3];[5;6];[4;1]] http://www.cs.ucsd.edu/classes/fa06/cse130/lectures/s1.ml [2/13/2008 5:18:01 PM]...
View Full Document

This note was uploaded on 02/14/2008 for the course CSE 130 taught by Professor Jhala during the Fall '06 term at UCSD.

Ask a homework question - tutors are online