Section Syntax Errors

Hi all, I am learning about sections and currying from SOE. There are three functions below. The first is from SOE and the other two are my own practice functions. Why does the third function fail? --Test for negative numbers (from SOE pg 109). This works posInts :: [Integer] -> [Bool] posInts = map (> 0) --Add 1 to a list of numbers. This works. addOne :: [Integer] -> [Integer] addOne = map (+ 1) --Subtract 1 from a list of numbers. This does NOT work. --Fails in Hugs with: -- Instance of Num (Integer -> Integer) required for definition of testSections -- I tried : subOne :: Num a => [a] -> [a] -- and : subOne :: [Float] -> [Float] --in case the problem is with negative numbers. They both fail. --Without any type signature I get: -- Unresolved top level overloading subOne :: [Integer] -> [Integer] subOne = map (- 1) Thanks... Deech _________________________________________________________________ Add a Yahoo! contact to Windows Live Messenger for a chance to win a free trip! http://www.imagine-windowslive.com/minisites/yahoo/default.aspx?locale=en-us&hmtagline

Hi Deech,
Trying some things in Hugs:
Hugs> :t (+1)
flip (+) 1 :: Num a => a -> a
Hugs> :t (<0)
flip (<) 0 :: (Num a, Ord a) => a -> Bool
Hugs> :t (-1)
-1 :: Num a => a
If you see, Haskell interprets the first two as sections, but the last
one is treated as a unary minus, inside brackets - i.e. as though you
typed the numeric literal -1.
Haskell's treatment of negative literals is sometimes confusing...
Thanks
Neil
On 11/11/06, Aditya Siram
Hi all, I am learning about sections and currying from SOE. There are three functions below. The first is from SOE and the other two are my own practice functions. Why does the third function fail?
--Test for negative numbers (from SOE pg 109). This works posInts :: [Integer] -> [Bool] posInts = map (> 0)
--Add 1 to a list of numbers. This works. addOne :: [Integer] -> [Integer] addOne = map (+ 1)
--Subtract 1 from a list of numbers. This does NOT work. --Fails in Hugs with: -- Instance of Num (Integer -> Integer) required for definition of testSections -- I tried : subOne :: Num a => [a] -> [a] -- and : subOne :: [Float] -> [Float] --in case the problem is with negative numbers. They both fail. --Without any type signature I get: -- Unresolved top level overloading subOne :: [Integer] -> [Integer] subOne = map (- 1)
Thanks... Deech
_________________________________________________________________ Add a Yahoo! contact to Windows Live Messenger for a chance to win a free trip! http://www.imagine-windowslive.com/minisites/yahoo/default.aspx?locale=en-us&hmtagline
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 11/11/06, Aditya Siram
subOne :: [Integer] -> [Integer] subOne = map (- 1)
The short answer is that this is interpreted as negative unity, rather than a section of binary minus. There are two common workarounds: subOne = map (subtract 1) subOne = map (+ (-1)) There's a whole minefield of opinions on whether this is the right syntax or not. I'm sure you could google through the archives to research this a bit more. -- -David House, dmhouse@gmail.com

On Sat, 11 Nov 2006, David House wrote:
On 11/11/06, Aditya Siram
wrote: subOne :: [Integer] -> [Integer] subOne = map (- 1)
The short answer is that this is interpreted as negative unity, rather than a section of binary minus. There are two common workarounds:
subOne = map (subtract 1) subOne = map (+ (-1))
There's a whole minefield of opinions on whether this is the right syntax or not. I'm sure you could google through the archives to research this a bit more.
E.g. this thread http://www.haskell.org/pipermail/haskell-cafe/2006-August/017403.html continued here: http://www.haskell.org/pipermail/haskell-cafe/2006-September/017941.html
participants (4)
-
Aditya Siram
-
David House
-
Henning Thielemann
-
Neil Mitchell