1

I've just started learning Standard ML (and functional programming in general) and I've come across two different ways of defining a function.

val double = fn x => x*2:

And

fun double x = x*2;

If I understand correctly, the first one is assigning a variable to an ananonymous function. Under what circumstances should I do this instead of fun abc?

icedvariables
  • 181
  • 1
  • 8

1 Answers1

2

This is a style question. The fun syntax is syntactic sugar for fn, so anything that you can write with the former can also be written with the latter.

fn directly represents λ-abstraction, which means it is limited to functions of one argument (see this SO question). fun is convenient shorthand that allows you to curry a multi-argument function and bind a name to it with a single bit of syntax, so it's probably better to use fun whenever you want to do one of those things.

Community
  • 1
  • 1
sjy
  • 2,702
  • 1
  • 21
  • 22