3 Replies - 1480 Views - Last Post: 25 January 2013 - 01:32 PM

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

SML type of function (currying)?

Posted 25 January 2013 - 12:53 PM

Using ListPair.foldr I need to create a function zipWith that combines pairwise two lists. The type of the overall function should be:

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

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

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



My attempt:

fun zipWith f [] [] = []
  | ListPair.foldr f (x::xs) (y::ys) = f(x,(zipWith f ys d));



Is This A Good Question/Topic? 0
  • +

Replies To: SML type of function (currying)?

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: SML type of function (currying)?

Posted 25 January 2013 - 12:56 PM

As I explained in my comment to your SO question:

Quote

You're not supposed to define zipWith as a recursive function using pattern matching - you're supposed to be calling foldr. Also using the name of a different function as part of pattern matching is not valid. Your code should look like fun zipWith f xs ys = ListPair.foldr ... ;.

Was This Post Helpful? 1
  • +
  • -

#3 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

Re: SML type of function (currying)?

Posted 25 January 2013 - 01:23 PM

View Postsepp2k, on 25 January 2013 - 12:56 PM, said:

As I explained in my comment to your SO question:

Quote

You're not supposed to define zipWith as a recursive function using pattern matching - you're supposed to be calling foldr. Also using the name of a different function as part of pattern matching is not valid. Your code should look like fun zipWith f xs ys = ListPair.foldr ... ;.


Thanks for the answer but I still cant get it right, here is what I do:

fun zipWith f xs ys = ListPair.foldr(f(x,(zipWith f xs ys)));

This post has been edited by hey92: 25 January 2013 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: SML type of function (currying)?

Posted 25 January 2013 - 01:32 PM

Your attempt is still recursive and as I said, that's not what you're supposed to do. A recursive solution would not be calling foldr. The point here is to solve the problem just by calling foldr - no recursion.

You're also calling ListPair.foldr with the wrong number of arguments. It takes three arguments: a function, a starting value and a tuple containing two lists. You're only calling it with a single argument: the result of calling f.

You should think about what foldr does, how it works, and how that can be used to do what you're supposed to do. Then you should think about what the arguments to foldr should be to achieve this and then call it with those arguments.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1