wiki:Functions/Zip

zip

fun zip(x::xs,y::ys) = (x,y) :: zip(xs,ys)
  | zip _ = [];
zip [] _          =  []
zip _ []          =  []
zip (a:as) (b:bs) =  (a,b) : zip as bs
zip (x:xs) (y:ys)  =  (x,y) : zip xs ys
zip _ _            =  []

zip3 (a:as) (b:bs) (c:cs)  =  (a,b,c) : zip3 as bs cs
zip3 _      _      _       =  []

using list comprehensions

zip([X | Xs], [Y | Ys]) -> [{X, Y} | zip(Xs, Ys)];
zip([], [])             -> [].
zip3([X | Xs], [Y | Ys], [Z | Zs]) -> [{X, Y, Z} | zip3(Xs, Ys, Zs)];
zip3([], [], [])                   -> [].
exception ListLengthMismatch
  
fun zip3 xxx =
    case xxx of
         ([],[],[]) => []
       | (hd1::tl1,hd2::tl2,hd3::tl3) => (hd1,hd2,hd3)::zip3(tl1,tl2,tl3)
       |_ => raise ListLengthMismatch
fun unzip3 xss =
    case xss of
         [] => ([],[],[])
       | (x,y,z)::tl => let val (xs,ys,zs) = unzip3 tl
                        in
                          (x::xs,y::ys,z::zs)
                        end
Last modified 16 months ago Last modified on Oct 24, 2018, 11:06:01 AM
Note: See TracWiki for help on using the wiki.