4 Replies - 1516 Views - Last Post: 31 January 2013 - 09:23 PM

#1 hey92  Icon User is offline

  • New D.I.C Head

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

SML/NJ higher order function?

Posted 30 January 2013 - 09:58 AM

I'm trying to implement a function

ziprev : 'a list -> 'b list -> ('a * 'b)list
- ziprev [1,2,3,4] [10,20,30,40];
val it = [(1,40),(2,30),(3,20),(4,10)] : (int * int) list



Using zipWith and the List.rev from the library.

Attempt:
fun ziprev xs ys = List.rev(List.zipWith SOMETHING xs);



I'm confused as to what to put in something, i don't think it's a function, i tried ys but the type is not the same, everything else I tried gives me error.

This post has been edited by hey92: 30 January 2013 - 09:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SML/NJ higher order function?

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: SML/NJ higher order function?

Posted 30 January 2013 - 10:46 AM

zipWith takes three arguments: a function and two lists. It will produce a list where each element is the result of calling the function with the elements at the same index at the first and second list respectively as arguments.

So yes, you do need to put a function in place of "SOMETHING". You also need to add a second list as a third argument after xs.

Oh, and you should think about what the result of calling zipWith will look like and what you want to reverse when to get the result that you need.

This post has been edited by sepp2k: 30 January 2013 - 10:51 AM

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/NJ higher order function?

Posted 31 January 2013 - 11:53 AM

View Postsepp2k, on 30 January 2013 - 10:46 AM, said:

zipWith takes three arguments: a function and two lists. It will produce a list where each element is the result of calling the function with the elements at the same index at the first and second list respectively as arguments.

So yes, you do need to put a function in place of "SOMETHING". You also need to add a second list as a third argument after xs.

Oh, and you should think about what the result of calling zipWith will look like and what you want to reverse when to get the result that you need.


I'm still having trouble defining the function. Operator : 'Z list Operand: 'Y * 'X -> ('Y * 'X) list

fun ziprev xs ys = List.zipWith(List.rev(fn(a,B)/>) => (a,B)/>::[]),xs,ys);



View Postsepp2k, on 30 January 2013 - 10:46 AM, said:

zipWith takes three arguments: a function and two lists. It will produce a list where each element is the result of calling the function with the elements at the same index at the first and second list respectively as arguments.

So yes, you do need to put a function in place of "SOMETHING". You also need to add a second list as a third argument after xs.

Oh, and you should think about what the result of calling zipWith will look like and what you want to reverse when to get the result that you need.


I'm still having trouble defining the function. Operator : 'Z list Operand: 'Y * 'X -> ('Y * 'X) list

fun ziprev xs ys = List.zipWith(List.rev(fn(a,B)/>) => (a,B)/>::[]),xs,ys);


Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: SML/NJ higher order function?

Posted 31 January 2013 - 08:50 PM

List.rev does not take a function as its argument - it takes a list. Further List.rev produces a list, but the first argument to zipWith should be a function.

This post has been edited by sepp2k: 31 January 2013 - 08:52 PM

Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: SML/NJ higher order function?

Posted 31 January 2013 - 09:23 PM

I also want to point out that it's not fair to the people helping you if you ask the same question in multiple places on the web. Especially without giving any indication that you've done so. That just leads to people wasting their time by giving you advice that you've already been given elsewhere.

The same is even more true about asking the same question again without taking into account advice you've already been given when you've asked the question previously.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1