[Haskell-cafe] Currying function using values from array

Tom Nielsen tanielsen at gmail.com
Thu Aug 7 15:49:57 EDT 2008


Maybe you want something like

curryWithList :: ([a]->b)->[a]->([a]->b)
curryWithList f lst1= \lst2 ->f (lst1++lst2)

addThemUp = sum
curried = curryWithList addThemUp [1,2,3,4]
curried [5] =15

On Thu, Aug 7, 2008 at 8:35 PM, Henning Thielemann
<lemming at henning-thielemann.de> wrote:
>
> 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.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list