[MLton] sequences of products

Vesa Karvonen vesa.karvonen@cs.helsinki.fi
Tue, 20 Sep 2005 14:23:51 +0300


Quoting Stephen Weeks <sweeks@sweeks.com>:
[...]
>   p (fn x1 => fn x2 => ... fn xn => e)
>
> The currying is not so concise in SML.  One could use a function
> declaration instead of an anonymous function, but for many situations
> anonymous functions are handy.  So, I think it's useful to have a
> family of curry functions C<n> so that one can do
> 
>   p (Cn (fn (x1, x2, ..., xn) => e))
[...]
> fun C2 z a b = z (a, b)
> fun C3 z a b c = z (a, b, c)
> fun C4 z a b c d = z (a, b, c, d)
> fun C5 z a b c d e = z (a, b, c, d, e)
> fun C6 z a b c d e f = z (a, b, c, d, e, f)
> fun C7 z a b c d e f g = z (a, b, c, d, e, f, g)
> fun C8 z a b c d e f g h = z (a, b, c, d, e, f, g, h)
> fun C9 z a b c d e f g h i = z (a, b, c, d, e, f, g, h, i)

Just a quick note. Using product type, you can get a similar effect
using a linear implementation (the types will still be O(n*n)):

  datatype ('a, 'b) product = & of 'a * 'b
  infix &

  fun C2 f x y = f (x & y)
  fun C3 f = C2 (C2 f)
  fun C4 f = C2 (C3 f)
  fun C5 f = C2 (C4 f)
  fun C6 f = C2 (C5 f)
  fun C7 f = C2 (C6 f)
  fun C8 f = C2 (C7 f)
  fun C9 f = C2 (C8 f)

Now you could write

  p (Cn (fn x1 & x2 & ... & xn => e))

instead of

  p (Cn (fn (x1, x2, ..., xn) => e))

-Vesa Karvonen