[Haskell-cafe] Currying function using values from array
Bulat Ziganshin
bulat.ziganshin at gmail.com
Fri Aug 8 01:49:45 EDT 2008
Hello Sukit,
Friday, August 8, 2008, 3:52:51 AM, you wrote:
it requires use of typeclasses
instance C f => C (a->f)
curry (somef::(a->f)) (someas::[a]) =
(somef (head someas)) (tail someas)
and so on. look into hslua sources, for example:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hslua
> Thanks Tom and Henning for your response. Let me put the question
> in another way by generalizing and tweaking it a little bit.
> How in Haskell that I can create a function that curries any other
> function, which receives multiple parameters, by using a the input
> from a list (same data type) or a tuple (mixed data type) such that
> it either returns another closure (if not all parameters are
> curried) or the final value of the computation (when all parameters are known)?
>
> Ed
> On Thu, Aug 7, 2008 at 12:49 PM, Tom Nielsen <tanielsen at gmail.com> wrote:
> 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
>>
>
>
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Haskell-Cafe
mailing list