sml - Zip and unzip lists in Standard ML -
how create function zip , unzip 2 lists tupled lists in standard ml?
example:
unzip [[1,4],[2,5],[3,6]] -> [1,2,3] [4,5,6] zip [1,2,3] [0,2,4] -> [[1,0],[2,2],[3,4]]
it not idea use head
, tail
, instead use pattern matching. can encode unzip bit more elegantly follows:
fun unzip l = case l of nil => (nil, nil) | (a,b)::tl => let val (l1, l2) = unzip tl in (a::l1, b::l2) end
also 1 of commenters above mentioned, zip , unzip typically work on pairs of lists, , lists of pairs respectively.
Comments
Post a Comment