[Haskell-cafe] Re: [Haskell-beginners] map question
Job Vranish
jvranish at gmail.com
Thu Sep 17 12:04:02 EDT 2009
(-) happens to be the only prefix operator in haskell, it also an infix
operator.
so:
> 4 - 2
2
> -3
-3
> ((-) 5) 3 -- note that in this case (-) is treated like any regular
function so 5 is the first parameter
2
> (5 - ) 3
2
> (-5 )
-5
> (flip (-) 5) 3
-2
It's a little wart brought about by the ambiguity in common mathematical
syntax.
If you play around in ghci you should get the hang of it pretty quick.
- Job
On Thu, Sep 17, 2009 at 11:08 AM, Gregory Propf <gregorypropf at yahoo.com>wrote:
> Remember that there is asymmetry between (+) and (-). The former has the
> commutative property and the latter does not so:
>
> (+) 3 4 = 7
>
> and
>
> (+) 4 3 = 7
>
> but
>
> (-) 3 4 = -1
>
> and
>
> (-) 4 3 = 1
>
> --- On *Thu, 9/17/09, Tom Doris <tomdoris at gmail.com>* wrote:
>
>
> From: Tom Doris <tomdoris at gmail.com>
> Subject: Re: [Haskell-beginners] map question
> To: "Joost Kremers" <joostkremers at fastmail.fm>
> Cc: beginners at haskell.org
> Date: Thursday, September 17, 2009, 6:06 AM
>
> This works:
>
> map (+ (-1)) [1,2,3,4]
>
>
> 2009/9/17 Joost Kremers <joostkremers at fastmail.fm<http://mc/compose?to=joostkremers@fastmail.fm>
> >
>
>> 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 at haskell.org <http://mc/compose?to=Beginners@haskell.org>
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org <http://mc/compose?to=Beginners@haskell.org>
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090917/dec99a61/attachment.html
More information about the Haskell-Cafe
mailing list