[Haskell-cafe] Newbie: Applying Unknown Number Arguments to A
Partial Function
Arthur Baars
arthurb at cs.uu.nl
Fri May 19 05:45:52 EDT 2006
My apologies to Chris, I think I misinterpreted Aditya's description.
Thanks to David House for telling me. I thought he was describing a
function such as map instead of "polyvaric functions", which would have
been more likely for a "newbie" :-)
So to answer Aditya's question, whether you can do this in Haskell. The
short answer is "no". You cannot do this in plain Haskell. However
using various extensions to Haskell, you can indeed use the smart
"tricks" as Chris pointed out:
http://okmij.org/ftp/Haskell/types.html
Cheers,
Arthur
On 19-mei-06, at 11:24, Arthur Baars wrote:
> Chris, the subject states clearly that Aditya is a Newbie, and is most
> likely just trying to define the function "map". So I think pointing
> to a bunch of advanced type magic tricks is not really helpful.
>
> Aditya, you say you want the function applyArgument to take a function
> and a list and apply the function to all elements of that list. The
> result of "applyArgument" is a list, with the results of applying the
> function to each element.
>
> So you want the type of "applyArgument" to be:
> applyArgument :: (a->b) -> [a] -> [b]
>
>
> There are a numbers of errors in your code.
> applyArgument f (arg) = f arg
> The variable "f" is the first parameter of "applyArgument", and has
> type (a -> b)
> The variable "arg" is the second parameter of "applyArgument", and has
> type [a] .
> You try to apply "f" to a list which is not ok.
> Most likely you meant "[arg]", which is a singleton list, instead of
> "(arg)", which is the same as just "arg"
> Furthermore "applyArgument" returns a list of result. The function "f"
> only yields a "b", instead of a list "[b]"
>
> The following does work:
> applyArgument f [arg] = [f arg]
>
> In your second line:
> applyArgument f (arg:args) = applyArgument (f arg) args
> you use a list pattern "(arg:args)", which is good. The variable "arg"
> is the head of the list, and the variable "args" is the tail of the
> list. So "arg" has type "a", and "args" has type "[a]" . The
> application "(f arg)" has type "b". Because the function
> "applyArgument" expects a function as first argument and not a "b", so
> this is wrong. I guess you can find out by your self how to fix this.
> There are a number of very good tutorials for beginners on
> http://haskell.org .
>
> Finally your function won't work for empty lists, it is only defined
> for singleton lists and longer lists. You can fix this by replacing
> the definition for singleton lists with a definition for the empty
> list. The pattern for empty list is simply "[]".
>
> Good luck,
>
> Arthur
>
>
> On 19-mei-06, at 10:10, Chris Kuklewicz wrote:
>
>> Aditya Siram wrote:
>>> I am trying to write a function 'applyArguments' which takes a
>>> function
>>> and a list and recursively uses element each in the list as an
>>> argument
>>> to the function. I want to do this for any function taking any
>>> number of
>>> arguments.
>>>
>>> applyArgument f (arg) = f arg
>>> applyArgument f (arg:args) = applyArgument (f arg) args
>>>
>>> This has failed in Hugs, so my question is: Can I conceptually do
>>> this?
>>> If so, what is the type signature of this function?
>>>
>>> Deech
>>>
>>>
>>
>> You can't do that, but there are other tricks that do work:
>>
>> http://okmij.org/ftp/Haskell/types.html
>>
>> which describes "Functions with the variable number of (variously
>> typed)
>> arguments" and "Genuine keyword arguments"
>>
>> --
>> Chris
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
> _______________________________________________
> 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