[Haskell-cafe] Newbie: Applying Unknown Number Arguments to
A Partial Function
Robert Dockins
robdockins at fastmail.fm
Fri May 19 15:16:31 EDT 2006
On May 19, 2006, at 2:49 PM, Jeremy Shaw wrote:
> Hello,
>
> You can do it -- but it may not be very useful in its current
> form. The primary problem is, "What is the type of 'f'?"
>
>> applyArgument f [arg] = f arg -- NOTE: I changed (arg) to [arg]
>> applyArgument f (arg:args) = applyArgument (f arg) args
>
> Looking at the second line, it seems that f is a function that takes a
> value and returns a function that takes a value and returns a function
> that takes a value, etc. Something like:
>
> f :: a -> (a -> (a -> (a -> ...)))
>
> This is called an 'infinite type' and is not allowed in haskell (or
> ocaml by default) because it allows you to introduce type errors that
> the compiler can not catch:
>
> http://groups.google.com/group/comp.lang.functional/browse_thread/
> thread/3646ef7e64124301/2a3a33bfd23a7184
>
> If you introduce a wrapper type, you can make the type checker happy:
>
> newtype F a = F { unF :: a -> F a }
>
> applyArgument :: F a -> [a] -> F a
> applyArgument (F f) [arg] = f arg
> applyArgument (F f) (arg:args) = applyArgument (f arg) args
>
> Of course, your final result is still something of type 'F a' -- so it
> is probably not very useful -- because all you can do is apply it more
> more things of type a and get more things of type 'F a'.
>
> One option would be to modify the function to return a result and a
> continuation:
>
> newtype F a = F { unF :: a -> (a, F a) }
>
> applyArgument :: F a -> [a] -> a
> applyArgument (F f) [arg] = fst (f arg)
> applyArgument (F f) (arg:args) = applyArgument (snd (f arg)) args
>
> Then you define a function like this (a simple sum function in this
> case):
>
> f :: (Num a) => a -> (a, F a)
> f a' = (a', F $ \a -> f (a + a'))
>
> example usage:
>
> *Main> applyArgument (snd (f 0)) [1,2,3]
> 6
This seems like it is just an ugly way to spell 'foldl'. Is there
something fundamentally different about this approach, besides the
slightly odd typing? I understand its relation to the OP, but I'm
just curious now...
> Here is another variation that allows for 0 or more arguments instead
> of 1 or more:
>
> newtype F a = F { unF :: (a, a -> F a) }
>
> applyArgument :: F a -> [a] -> a
> applyArgument (F (result, _)) [] = result
> applyArgument (F (_ , f)) (arg:args) = applyArgument (f arg) args
>
> f :: (Num a) => a -> F a
> f a' = F (a', \a -> f (a + a'))
>
> j.
>
>
>
> At Fri, 19 May 2006 02:25:31 +0000,
> 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
Rob Dockins
Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
-- TMBG
More information about the Haskell-Cafe
mailing list