TemporaryFix
TemporaryFix

Reputation: 2186

How to use map in a function

The function in map is pretty easy. I want to double every element in a list which can be done:

map(fn x => x * 2);

But what if I want to name this function double?

fun double = map(fn x => x * 2);

Calling this function I get

- double [1,2,3];
val it = fn : int list -> int list

How can I name this function double?

Upvotes: 1

Views: 91

Answers (1)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

The result of map (fn x => x * 2) is a function, which can be bound to an identifier:

- val double = map (fn x => x * 2);
val double = fn : int list -> int list

- double [1,2,3];
val it = [2,4,6] : int list

The fun form is just syntactic sugar. For example:

fun name param = ...

will be desugared to:

val rec name = fn param => ...

The rec part is a keyword that lets you implement recursive function definitions.

Upvotes: 1

Related Questions