[Haskell-cafe] Re: [Haskell-beginners] map question

Gregory Propf gregorypropf at yahoo.com
Thu Sep 17 22:32:32 EDT 2009


Heh, perhaps we should petition to have a new computer key and symbol added to the world's way of writing maths, something like maybe a downward angled slash to mean prefix (-) 

:)

--- On Thu, 9/17/09, Job Vranish <jvranish at gmail.com> wrote:

From: Job Vranish <jvranish at gmail.com>
Subject: Re: [Haskell-cafe] Re: [Haskell-beginners] map question
To: "Gregory Propf" <gregorypropf at yahoo.com>
Cc: "Tom Doris" <tomdoris at gmail.com>, "Haskell-Cafe" <haskell-cafe at haskell.org>, joostkremers at fastmail.fm
Date: Thursday, September 17, 2009, 9:04 AM

(-) 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>


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://www.haskell.org/mailman/listinfo/beginners




-----Inline Attachment Follows-----

_______________________________________________
Beginners mailing list
Beginners at 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/e391ca65/attachment-0001.html


More information about the Haskell-Cafe mailing list