
Hi all, I've just started learning Haskell and while experimenting with map a bit, I ran into something I don't understand. The following commands do what I'd expect: Prelude> map (+ 1) [1,2,3,4] [2,3,4,5] Prelude> map (* 2) [1,2,3,4] [2,4,6,8] Prelude> map (/ 2) [1,2,3,4] [0.5,1.0,1.5,2.0] Prelude> map (2 /) [1,2,3,4] [2.0,1.0,0.6666666666666666,0.5] But I can't seem to find a way to get map to substract 1 from all members of the list. The following form is the only one that works, but it doesn't give the result I'd expect: Prelude> map ((-) 1) [1,2,3,4] [0,-1,-2,-3] I know I can use an anonymous function, but I'm just trying to understand the result here... I'd appreciate any hints to help me graps this. TIA Joost -- Joost Kremers, PhD University of Frankfurt Institute for Cognitive Linguistics Grüneburgplatz 1 60629 Frankfurt am Main, Germany

Am Donnerstag 17 September 2009 13:31:38 schrieb Joost Kremers:
Hi all,
I've just started learning Haskell and while experimenting with map a bit, I ran into something I don't understand. The following commands do what I'd expect:
Prelude> map (+ 1) [1,2,3,4] [2,3,4,5] Prelude> map (* 2) [1,2,3,4] [2,4,6,8] Prelude> map (/ 2) [1,2,3,4] [0.5,1.0,1.5,2.0] Prelude> map (2 /) [1,2,3,4] [2.0,1.0,0.6666666666666666,0.5]
But I can't seem to find a way to get map to substract 1 from all members of the list. The following form is the only one that works, but it doesn't give the result I'd expect:
Prelude> map ((-) 1) [1,2,3,4] [0,-1,-2,-3]
I know I can use an anonymous function, but I'm just trying to understand the result here... I'd appreciate any hints to help me graps this.
(-) a b = a - b, so (((-) 1) x) = 1 - x and you've mapped (\x -> 1-x) over the list. You want to map (\x -> x-1), which is map (subtract 1) list or map (flip (-) 1) list (or map (+ (-1)) list)
TIA
Joost

On Thu, Sep 17, 2009 at 01:50:27PM +0200, Daniel Fischer wrote:
Prelude> map ((-) 1) [1,2,3,4] [0,-1,-2,-3]
I know I can use an anonymous function, but I'm just trying to understand the result here... I'd appreciate any hints to help me graps this.
(-) a b = a - b, so (((-) 1) x) = 1 - x and you've mapped (\x -> 1-x) over the list.
Ah yes... Of course, I didn't realise that wrapping a binary operator in parens turns it into a normal function. And from http://www.haskell.org/onlinereport/exps.html#sect3.5, which someone pointed me to off-list, I gather that in (- 1) the minus sign is interpreted as the unary operator, not the binary one. Which means that (- 1) is not a function, which means map will barf. Ok, thanks, all makes sense now! Joost -- Joost Kremers Life has its moments

On Thu, Sep 17, 2009 at 01:31:38PM +0200, Joost Kremers wrote:
But I can't seem to find a way to get map to substract 1 from all members of the list. The following form is the only one that works, but it doesn't give the result I'd expect:
Prelude> map ((-) 1) [1,2,3,4] [0,-1,-2,-3]
By the way, the reason map (+1) [1,2,3,4] works but map (-1) [1,2,3,4] doesn't is because of an ugly corner of Haskell syntax: -1 here is parsed as negative one, rather than an operator section with subtraction. The 'subtract' function is provided exactly for this purpose, so that you can write map (subtract 1) [1,2,3,4] instead. -Brent

Brent Yorgey
By the way, the reason
map (+1) [1,2,3,4]
works but
map (-1) [1,2,3,4]
doesn't is because of an ugly corner of Haskell syntax: -1 here is parsed as negative one, rather than an operator section with subtraction. The 'subtract' function is provided exactly for this purpose, so that you can write
map (subtract 1) [1,2,3,4]
Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ? I know this doesn't parse, my question is, why wouldn't it be made valid syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ?

2009/10/17 Will Ness
Brent Yorgey
writes: By the way, the reason
map (+1) [1,2,3,4]
works but
map (-1) [1,2,3,4]
doesn't is because of an ugly corner of Haskell syntax: -1 here is parsed as negative one, rather than an operator section with subtraction. The 'subtract' function is provided exactly for this purpose, so that you can write
map (subtract 1) [1,2,3,4]
Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ?
I know this doesn't parse, my question is, why wouldn't it be made valid syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
`This` syntax is used to make functions work infix, whereas operators already are infix. For this reason, it wouldn't make much sense to use `this` syntax (what's that called anyways?) on an operator. `(-)` would in some sense be the same thing as just "-", since () is used to make operators prefix and `` is used to make functions infix. -- Deniz Dogan

Deniz Dogan
`This` syntax is used to make functions work infix, whereas operators already are infix. For this reason, it wouldn't make much sense to use `this` syntax (what's that called anyways?) on an operator.
It would be a no-op in general, so nobody would use it.
`(-)` would in some sense be the same thing as just "-", since () is used to make operators prefix and `` is used to make functions infix.
exactly, just that it would be a _binary_ (-) .

2009/10/18 Will Ness
Deniz Dogan
writes: `This` syntax is used to make functions work infix, whereas operators already are infix. For this reason, it wouldn't make much sense to use `this` syntax (what's that called anyways?) on an operator.
It would be a no-op in general, so nobody would use it.
`(-)` would in some sense be the same thing as just "-", since () is used to make operators prefix and `` is used to make functions infix.
exactly, just that it would be a _binary_ (-) .
So the only case where `(this)` syntax would be useful is where you want to make GHC understand you mean the binary (-) operator and you want to use point-free code. Now you have the choice between `(-)` and subtract, which one will it be? :) -- Deniz Dogan

Deniz Dogan
2009/10/18 Will Ness
: exactly, just that it would be a _binary_ (-) .
So the only case where `(this)` syntax would be useful is where you want to make GHC understand you mean the binary (-) operator and you want to use point-free code. Now you have the choice between `(-)` and subtract, which one will it be? :)
subtract = flip (-) -- binary (`-`) = (-) -- binary The only one place where I'd like to use it, is in (`-`2), instead of (flip (-) 2) or (subtract 2) or (\a->a-2) or anything else. Also, it is not `(-)` syntax that I'm advocating for, but `-` syntax, in sections, to guide the selection of the operator's _binary_ version over its _unary_ version. Actually, I suspect there is no unary (-), but rather in (-2) the "-" is a part of number read syntax. Having it enclosed in backticks would stop it being read as a part of a number, and thus ensure it being correctly interpreted as an operator, binary as any other Haskell operator: Prelude> :t (-) (-) :: (Num a) => a -> a -> a Prelude> :t (`-`2) <interactive>:1:2: parse error on input `-' Even (`-`) may be left as invalid syntax, as it is today: Prelude> :t (`foldl`) <interactive>:1:8: parse error on input `)' Prelude> :t (`foldl`0) (`foldl`0) :: (Num a) => (a -> b -> a) -> [b] -> a

On Sat, Oct 17, 2009 at 06:29:39PM +0000, Will Ness wrote:
Brent Yorgey
writes: By the way, the reason
map (+1) [1,2,3,4]
works but
map (-1) [1,2,3,4]
doesn't is because of an ugly corner of Haskell syntax: -1 here is parsed as negative one, rather than an operator section with subtraction. The 'subtract' function is provided exactly for this purpose, so that you can write
map (subtract 1) [1,2,3,4]
Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ?
I know this doesn't parse, my question is, why wouldn't it be made valid syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ?
`backticks` are only for making (prefix) functions into (infix) operators. - is already an infix operator, so putting it in backticks would be redundant. As for `(-)`, arbitrary expressions cannot go inside backticks, and for good reason: what would `(2 `mod`)` parse as? However, certainly different choices might have been possible. -Brent

Brent Yorgey
On Sat, Oct 17, 2009 at 06:29:39PM +0000, Will Ness wrote:
Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ?
I know this doesn't parse, my question is, why wouldn't it be made valid syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ?
`backticks` are only for making (prefix) functions into (infix) operators. - is already an infix operator, so putting it in backticks would be redundant. As for `(-)`, arbitrary expressions cannot go inside backticks, and for good reason: what would `(2 `mod`)` parse as?
However, certainly different choices might have been possible.
-Brent
backticks could've been made no-op for operators. What's so wrong with (`:`[])? It'd just be the same as (:[]). Except for `-`, where it would finally provide us with possibility to write a shortcut for the ugly (flip (-) 1) as (`-`1). (2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside backticks would require it be a binary infix op, causing a type mis-match. For (-) it could choose the binary version over the unary. Or it could stay illegal for the parenthesised expressions, and just made legal syntax for operators, as (`:`[]). I don't know how easy or even possible it is to implement; I'm just saying it makes sense, for me.

Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness:
Brent Yorgey
writes: On Sat, Oct 17, 2009 at 06:29:39PM +0000, Will Ness wrote:
Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ?
I know this doesn't parse, my question is, why wouldn't it be made valid syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ?
`backticks` are only for making (prefix) functions into (infix) operators. - is already an infix operator, so putting it in backticks would be redundant. As for `(-)`, arbitrary expressions cannot go inside backticks, and for good reason: what would `(2 `mod`)` parse as?
However, certainly different choices might have been possible.
-Brent
backticks could've been made no-op for operators. What's so wrong with (`:`[])? It'd just be the same as (:[]).
Makes writing the parser more complicated. Makes reading code more irritating.
Except for `-`, where it would finally provide us with possibility to write a shortcut for the ugly (flip (-) 1) as (`-`1).
That's ugly too. My favourite is subtract 1.
(2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside backticks would require it be a binary infix op, causing a type mis-match.
instance (Integral a) => Integral (b -> a) where ... Evil, yes, but then f `(2 `mod`)` x would type-check. But anyway, there are operators where a backticked section would type-check: f `(g `.`)` x That's not good.
For (-) it could choose the binary version over the unary.
Or it could stay illegal for the parenthesised expressions, and just made legal syntax for operators, as (`:`[]).
I don't know how easy or even possible it is to implement; I'm just saying it makes sense, for me.
But it has downsides.

Daniel Fischer
Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness:
backticks could've been made no-op for operators. What's so wrong with (`:`[])? It'd just be the same as (:[]).
Makes writing the parser more complicated. Makes reading code more irritating.
I suspected as much, but if it has some benefits, that would be justified.
Except for `-`, where it would finally provide us with possibility to write a shortcut for the ugly (flip (-) 1) as (`-`1).
That's ugly too. My favourite is subtract 1.
We could write (hypothetically) 'concatenate' for ++ too, but we don't. What's so great with writing out long names, that should be processed, mentally, to get at their meaning, when we have this nice operators syntax which makes the meaning immediately - visibly - apparent? The only problem in Haskell syntax is this unary-over-binary (-), so allowing backticks for operators as well as for regular names would finally provide a solution for it. Nobody would use it except for (-) sections. Or even, hypothetically, it could be used to allow unary operators in the language, in general. That whould be a mechanism for distinguishing the binary from the unary versions. Why not? Difficulty of writing a parser is not a valid reason if there's a clear utility in it. IMHO.

Am Sonntag 18 Oktober 2009 22:40:46 schrieb Will Ness:
Daniel Fischer
writes: Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness:
backticks could've been made no-op for operators. What's so wrong with (`:`[])? It'd just be the same as (:[]).
Makes writing the parser more complicated. Makes reading code more irritating.
I suspected as much, but if it has some benefits, that would be justified.
I think you mean "if the benefits outweigh the downsides". If they do (or rather, if enough people believe they do and few enough believe it's the other way round), then it's justified. As of now, I don't believe it has substantial benefits (consequences which I regard as benefits).
Except for `-`, where it would finally provide us with possibility to write a shortcut for the ugly (flip (-) 1) as (`-`1).
That's ugly too. My favourite is subtract 1.
We could write (hypothetically) 'concatenate' for ++ too, but we don't. What's so great with writing out long names, that should be processed, mentally, to get at their meaning, when we have this nice operators syntax which makes the meaning immediately - visibly - apparent?
Operator syntax per se doesn't make the meaning immediately apparent. Only the meaning of well known operators (like +,-,*,/) and, to some extent, close derivations of them (like ++) is readily apparent. ($) or (>>=) aren't obvious when you first see them. However, I didn't want to advocate the use of alphabetic identifiers instead of operators, I meant that since (- 1) doesn't work, my favourite way to express it is (subtract 1). I find that nicer than (flip (-) 1), and also aesthetically superior to (`-` 1). Purely personal taste, of course.
The only problem in Haskell syntax is this unary-over-binary (-), so
Yes, that's unpleasant. However, for me it's a minor point.
allowing backticks for operators as well as for regular names would finally provide a solution for it.
Nobody would use it except for (-) sections.
Are you sure? People abuse every oportunity you give them and then some, so I wouldn't be too surprised if gratuitous backticks seeped into Haskell code over time.
Or even, hypothetically, it could be used to allow unary operators in the language, in general. That whould be a mechanism for distinguishing the binary from the unary versions.
Why not? Difficulty of writing a parser is not a valid reason if there's a clear utility in it. IMHO.

Daniel Fischer
Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness:
(2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside backticks would require it be a binary infix op, causing a type mis-match.
instance (Integral a) => Integral (b -> a) where ...
Evil, yes, but then f `(2 `mod`)` x would type-check.
Any problem that could be introduced for operators could be introduced for the regular named values as well.
But anyway, there are operators where a backticked section would type-check:
f `(g `.`)` x
That's not good.
Why? Is it not just (g .) f x ? What's the problem with it? But, the backticked parens syntax is less necessary. What I'd really like is for (`+`1) syntax to become legal. Prelude> :t let add=(+) in (`add`2) let add=(+) in (`add`2) :: (Num a) => a -> a Are we not supposed to be able to substitute equal for equal in Haskell? Prelude> :t (`(+)`2) <interactive>:1:2: parse error on input `(' Prelude> :t (`+`2) <interactive>:1:2: parse error on input `+'

Am Sonntag 18 Oktober 2009 22:57:38 schrieb Will Ness:
Daniel Fischer
writes: Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness:
(2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside backticks would require it be a binary infix op, causing a type mis-match.
instance (Integral a) => Integral (b -> a) where ...
Evil, yes, but then f `(2 `mod`)` x would type-check.
Any problem that could be introduced for operators could be introduced for the regular named values as well.
But allowing only one level of backticks, and only on plain identifiers limits the scope of possible problems.
But anyway, there are operators where a backticked section would type-check:
f `(g `.`)` x
That's not good.
Why? Is it not just (g .) f x ? What's the problem with it?
It's incredibly ugly, looks almost like a perl regex.

This works:
map (+ (-1)) [1,2,3,4]
2009/9/17 Joost Kremers
Hi all,
I've just started learning Haskell and while experimenting with map a bit, I ran into something I don't understand. The following commands do what I'd expect:
Prelude> map (+ 1) [1,2,3,4] [2,3,4,5] Prelude> map (* 2) [1,2,3,4] [2,4,6,8] Prelude> map (/ 2) [1,2,3,4] [0.5,1.0,1.5,2.0] Prelude> map (2 /) [1,2,3,4] [2.0,1.0,0.6666666666666666,0.5]
But I can't seem to find a way to get map to substract 1 from all members of the list. The following form is the only one that works, but it doesn't give the result I'd expect:
Prelude> map ((-) 1) [1,2,3,4] [0,-1,-2,-3]
I know I can use an anonymous function, but I'm just trying to understand the result here... I'd appreciate any hints to help me graps this.
TIA
Joost
-- Joost Kremers, PhD University of Frankfurt Institute for Cognitive Linguistics Grüneburgplatz 1 60629 Frankfurt am Main, Germany _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Sep 17, 2009 at 2:06 PM, Tom Doris
This works:
map (+ (-1)) [1,2,3,4]
map pred [1..4] /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus@therning.org http://therning.org/magnus identi.ca|twitter: magthe

Cue The Evolution of a Haskell Programmer: http://www.willamette.edu/~fruehr/haskell/evolution.html :-) Magnus Therning wrote:
On Thu, Sep 17, 2009 at 2:06 PM, Tom Doris
wrote: This works:
map (+ (-1)) [1,2,3,4]
map pred [1..4]
/M
-- Tommy M. McGuire mcguire@crsr.net
participants (8)
-
Brent Yorgey
-
Daniel Fischer
-
Deniz Dogan
-
Joost Kremers
-
Magnus Therning
-
Tom Doris
-
Tommy M. McGuire
-
Will Ness