[Haskell-cafe] Currying function using values from array

Henning Thielemann lemming at henning-thielemann.de
Thu Aug 7 15:35:16 EDT 2008

On Thu, 7 Aug 2008, Sukit Tretriluxana wrote:

> Dear Haskell experts,
> I am currently studying Groovy language. An experiment I did with its
> closure is to perform closure/function "curry" using an array containing the
> values for the parameter binding. See the sample below.
> int addThemUp(a,b,c,d,e) { a+b+c+d+e }
> def arrayCurry(arr, cls) { arr.inject(cls) { c, v -> c.curry(v) } }
> println addThemUp(1,2,3,4,5)
> println arrayCurry([1,2,3,4,5], this.&addThemUp)()
> println arrayCurry([1,2,3,4], this.&addThemUp)(5)
> The printouts from the above code are the same, verifying that the code
> works fine. Then I come to ask myself how I can do the same in Haskell. I'm
> not a Haskell expert so I couldn't figure it. I wonder if you guys could
> shed some light on this.

I do not know Groovy, but maybe you want something like

  addThemUp :: Num a => (a,a,a,a,a) -> a
  addThemUp (a,b,c,d,e) = a+b+c+d+e

  -- should be better named list5Curry or so
  arrayCurry :: ((a,a,a,a,a) -> a) -> [a] -> a
  arrayCurry cls [a,b,c,d,e] = cls (a,b,c,d,e)

  print (addThemUp(1,2,3,4,5::Int))
  print (arrayCurry addThemUp [1,2,3,4,5::Int])

However, it's hardly of any use, since you won't use a list if the number
of elements is fixed (and small) or if the elements even must have
distinct types.

More information about the Haskell-Cafe mailing list