简体   繁体   中英

Using ListPair.foldr to implement zipWith in SML

Background: Beginner level at SML

My assignment requires me to use ListPair.foldr and only this function to implement the zipWith function.

ListPair.foldr : ('a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c
zipWith : ('a * 'b -> 'c) -> 'a list -> 'b list -> 'c list

ListPair.foldr returns a single 'c element, while zipWith returns a 'c list, so naturally my approach would use ListPair.foldr repeatedly to spill out individual 'c elements which I then put in my 'c list. ListPair.foldr takes a pair of lists and folds them over each other according to the supplied function, so the only way to get the desired effect would be to take one element from each list at a time, feed it to ListPair.foldr as a pair of lists, take the result, and concatenate it to the next round. I'd also have to convert the function from ('a*'b->'c) to ('a*'b*'c->'c). Like so:

fun zipWith f [] l2 = []
| zipWith f l1 l2 = 
let val f2 = fn (a,b,c)=> f(a,b)+c   (* converting the function *)
    val xh = [hd(l1)]      (*first element of 'a list, as a list itself *)
    val yh = [hd(l2)]      (*first element of 'b list, as a list itself *)
    val xt = tl(l1)        (*taking the tail of 'a list*)
    val yt = tl(l2)        (*taking the tail of 'b list*)
in
    ListPair.foldr f2 0 (xh, yh)::    (*perform the operation with the two heads*)
    zipWith f xt yt                   (*recursively call zipWith with the remainder*)
end;

This works.

- zipWith (fn (x,y)=>x+y) [1,2,3] [10,20,30];
val it = [11,22,33] : int list

But now the tricky part: I'm not supposed to do this recursively, that is, I can't call zipWith within my zipWith function. Is this even possible? From what I read, the actual zipWith function in Haskell is defined recursively; how do I do this non-recursively?

I can't imagine the professor urging us to do this in an object-oriented way with while loops and such (I tried anyway, my level is not adequate for even that).

Am I in the completely wrong direction? How should I approach this question?

-----------------Answered----------------

I actually tried pad's solution at first:

fun zipWith f l1 l2 = 
let val f2 = fn (a,b,c)=> f(a,b)::c
in
    ListPair.foldr f2 0 l1 l2
end;

But it didn't work because I was appending it to 0 instead of []. The types didn't work out and I couldn't figure it out!

Thank you!

Your approach is correct but unnecessarily complex. Function zipWith is recursive but you can define it non-recursively because ListPair.foldr already has recursive nature.

To get closer to zipWith , you need a specialized version of ListPair.foldr with the following signature

ListPair.foldr : 
   ('a * 'b * 'c list -> 'c list) -> 'c list -> 'a list * 'b list -> 'c list

It means you pass an empty list as an accumulator and build up bigger lists along the way. In zipWith f xs ys , f has 'a * 'b -> 'c signature, so it is very easy to adapt:

fun zipWith f xs ys =
    ListPair.foldr (fn (a, b, cs) => f(a, b)::cs) [] (xs, ys)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM