# SML/NJ higher order function?

Page 1 of 1

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

### #1 hey92

Reputation: 0
• 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

• D.I.C Lover

Reputation: 2270
• Posts: 3,483
• 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

### #3 hey92

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

## Re: SML/NJ higher order function?

Posted 31 January 2013 - 11:53 AM

sepp2k, 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);

```

sepp2k, 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);

```

### #4 sepp2k

• D.I.C Lover

Reputation: 2270
• Posts: 3,483
• 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

### #5 sepp2k

• D.I.C Lover

Reputation: 2270
• Posts: 3,483
• 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.