supplying some of the arguments to a function

if I had a function f that took x y and z in that order, is there some way that I can supply y and z and get back a function that takes x? This question comes about after talking with a prof about currying and wether it buys you anything. -mdg

On 2005 May 06 Friday 11:32, Mark Goldman wrote:
if I had a function f that took x y and z in that order, is there some way that I can supply y and z and get back a function that takes x? This question comes about after talking with a prof about currying and wether it buys you anything.
There's a standard function 'flip' which solves a somewhat simpler and more common form of this problem. If you have a function f that takes x and y, is there some way to supply y and get back a function that takes x? flip f y Flip is defined as flip f x y = f y x This technique can be extended to answer your question. It doesn't nail down the value of currying.

Hi Mark,
if I had a function f that took x y and z in that order, is there some way that I can supply y and z and get back a function that takes x?
Warning: I just began using Haskell last week. :-) The closest I can think of is this: (\x -> f x 2 3) (assuming you want y=2, z=3) But that's really defining a new function, so you might as well give it a name: g x = f x 2 3
This question comes about after talking with a prof about currying and wether it buys you anything.
I'm liking currying a lot, I must admit. :-) But the example above isn't currying AFAIK. Cheers, Daniel.

Mark Goldman wrote:
if I had a function f that took x y and z in that order, is there some way that I can supply y and z and get back a function that takes x? This question comes about after talking with a prof about currying and wether it buys you anything.
-mdg
let f = (\x y z -> ()) :: Bool -> Char -> Int -> () :t (\x -> f x 'a' 0) (\x -> f x 'a' 0) :: Bool -> () If you don't like lambdas you can do the same thing with combinators (eg, flip, (.), const, etc) :t (flip . flip f) 'a' 0 (flip . flip f) 'a' 0 :: Bool -> () Mostly, one should just try to write functions so that they take their parameters in the most convenient order for partial application. Robert Dockins

On Friday 06 May 2005 17:32, Mark Goldman wrote:
if I had a function f that took x y and z in that order, is there some way that I can supply y and z and get back a function that takes x? This question comes about after talking with a prof about currying and wether it buys you anything.
If you define rotl3 f x y z = f y z x -- rotate left first 3 arguments then, assuming f :: a -> b -> c -> d, rotl3 f :: b -> c -> a -> d and thus rotl3 f your_y your_z :: a -> d The already mentioned standard function 'flip' is equal to the left and right rotation for 2 arguments: flip = rotl2 = rotr2 = \f x y -> f y x You can imagine a family of 'rotl<n>' and 'rotr<n>' functions, indexed by the natural numbers. Unfortunately Haskell's type system(?) is not strong enough to declare the whole family at one stroke. Would it be useful to have the first 3 to 5 instances in the standard library, i.e. rotl3, rotr3, rotl4, rotr4, rotl5, rotr5? (You could also name them rotate_left3, ...) HTH Ben

Am Freitag, 6. Mai 2005 17:32 schrieb Mark Goldman:
if I had a function f that took x y and z in that order, is there some way that I can supply y and z and get back a function that takes x? This question comes about after talking with a prof about currying and wether it buys you anything.
-mdg
Prelude> :t (flip .) . flip (flip .) . flip :: (a -> b -> c -> d) -> b -> c -> a -> d Prelude> :t (((flip .) . flip) .) . flip (((flip .) . flip) .) . flip :: (a -> b -> c -> d -> e) -> b -> c -> d ->a->e and so on. But in general you'd be better of with g z x y = f x y z, let h y z x = f x y z in and the write map (g z x) or whatever. Cheers, Daniel
participants (6)
-
Benjamin Franksen
-
Daniel Carrera
-
Daniel Fischer
-
Mark Goldman
-
robert dockins
-
Scott Turner