Re: [Haskell-cafe] Currying: The Rationale

Is (^2) really considered currying? As I understand it, this is syntactic sugar for a "section", and might confuse the issue a bit, since it's distinct from ((^) 2). In this case we would have something like Prelude> let pow2 = ((^) 2) Prelude> map pow2 [1..10] [2,4,8,16,32,64,128,256,512,1024] I think it's also worth pointing out that currying can make point-free programming more natural. So, foldl has type Prelude> :t foldl foldl :: (a -> b -> a) -> a -> [b] -> a That this could have been defined as foldlUncurried :: ((a -> b -> a), a, [b]) -> a but then simple point-free definitions like sum = foldl (+) 0 would have to be written as sum xs = foldlUncurried ((+), 0, xs) OTOH, sometimes an uncurried version is easier to reason about. So, in some of Richard Bird's work ("Algebra of Programming" is like this, and probably at least some of his intro book) examples are uncurried, as it shows more of the algebraic structure, and allows reasoning in a product space rather than an exponential. Umm, I guess that qualifies as jargon - sorry. Chad
(^) applied to 2, produces a new function, we can map over a list:
Prelude> let sq = (^2) Prelude> map sq [1..10] [1,4,9,16,25,36,49,64,81,100]
or more explicitly:
Prelude> let x `to` y = x ^ y Prelude> let sq x = x `to` 2 Prelude> map sq [1..10] [1,4,9,16,25,36,49,64,81,100]
-- Don

On Wed, 23 May 2007, Chad Scherrer wrote:
Is (^2) really considered currying? As I understand it, this is syntactic sugar for a "section", and might confuse the issue a bit, since it's distinct from ((^) 2).
Sure, but it's (flip (^)) 2. -- flippa@flippac.org Sometimes you gotta fight fire with fire. Most of the time you just get burnt worse though.

On 5/23/07, Philippa Cowderoy
On Wed, 23 May 2007, Chad Scherrer wrote:
Is (^2) really considered currying? As I understand it, this is syntactic sugar for a "section", and might confuse the issue a bit, since it's distinct from ((^) 2).
Sure, but it's (flip (^)) 2.
Well, ok, but you've changed the definition. If it were enough for it to be equivalent to a curried version, we could as well write sq x = times (x,x) where times (x,y) = x * y and argue that this is partial application of a curried function because it's equivalent to the curried version you gave. But I guess I'm being a bit pedantic here, and I suspect your definition is exactly how (^2) is desugared. Chad
-- flippa@flippac.org
Sometimes you gotta fight fire with fire. Most of the time you just get burnt worse though.

Disclaimer: I've not read the standard.
Sections are de-sugared depending on which argument you supply:
(x^) ==> (^) x
(^x) ==> flip (^) x
I think this is why they are considered special cases.
Prelude> map (^2) [1..10]
[1,4,9,16,25,36,49,64,81,100]
Prelude> map (flip (^) 2) [1..10]
[1,4,9,16,25,36,49,64,81,100]
Prelude> map (2^) [1..10]
[2,4,8,16,32,64,128,256,512,1024]
Prelude> map ((^) 2) [1..10]
[2,4,8,16,32,64,128,256,512,1024]
On 5/23/07, Chad Scherrer
On 5/23/07, Philippa Cowderoy
wrote: On Wed, 23 May 2007, Chad Scherrer wrote:
Is (^2) really considered currying? As I understand it, this is syntactic sugar for a "section", and might confuse the issue a bit, since it's distinct from ((^) 2).
Sure, but it's (flip (^)) 2.
Well, ok, but you've changed the definition. If it were enough for it to be equivalent to a curried version, we could as well write
sq x = times (x,x) where times (x,y) = x * y
and argue that this is partial application of a curried function because it's equivalent to the curried version you gave. But I guess I'm being a bit pedantic here, and I suspect your definition is exactly how (^2) is desugared.
Chad
-- flippa@flippac.org
Sometimes you gotta fight fire with fire. Most of the time you just get burnt worse though.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Chad Scherrer wrote:
On 5/23/07, Philippa Cowderoy
wrote: On Wed, 23 May 2007, Chad Scherrer wrote:
Is (^2) really considered currying? As I understand it, this is syntactic sugar for a "section", and might confuse the issue a bit, since it's distinct from ((^) 2).
Sure, but it's (flip (^)) 2.
Well, ok, but you've changed the definition. If it were enough for it to be equivalent to a curried version, we could as well write
sq x = times (x,x) where times (x,y) = x * y
and argue that this is partial application of a curried function because it's equivalent to the curried version you gave. But I guess I'm being a bit pedantic here, and I suspect your definition is exactly how (^2) is desugared.
Philippa's version is still an instance of partial application; no one said that the function must be a variable, an arbitrary expression is fine. 2 is being partially applied to flip (^) so defining sq makes no sense. Also there are no curried functions and no partial applications anywhere in your code.
participants (4)
-
Chad Scherrer
-
Derek Elkins
-
Nicolas Frisby
-
Philippa Cowderoy