Hi, I found while testing one of my functions for my coursework that there
appears to be a bug in the way Haskell interprets some input from the
command line, giving incorrect results from my function.
I will demonstrate this:
(i) CLEARLY THIS IS CORRECT:
Calculator> -1 - -23
22
(ii) HOWEVER, GIVEN THIS FUNCTION:
module calculator where
type OpCodes = String
(|$|) :: Float -> (OpCodes,Float) -> Float
(|$|) f1 (op,f2)
| op=="add_to" = f1 + f2
| op=="take_away" = f1 - f2
| op=="multiplied_by"= f1 * f2
| op=="divided_by" = f1 / f2
| otherwise = error( "undefined operation" ++ op)
(iii) THE FOLLOWING INCORRECT RESPONSE FROM HUGS OCCURS:
Calculator> -1 |$| ("take_away", -23)
-24.0
Now you can see there are two possibilities of what is going wrong,
either -1 - 23 is being worked out, or 1 - -23 is being worked out
and the result's sign is toggled. I believe it to be the later, (or
rather the toggling is apparant from appearance and in fact the minus
placed in front of the 1 in the first place is used in front of
the result).
Now, the next bit may help my theory stated...
(iv) IF YOU USE THE FUNCTION AS PREFIX:
Calculator> (|$|) -1 ("take_away", -23)
ERROR: Illegal Haskell 98 class constraint in inferred type
*** Expression : (|$|) - fromInt 1 ("take_away",-23)
*** Type : (Num (([Char],a) -> Float -> (OpCodes,Float) -> Float),
Num a, Num (Float -> (OpCodes,Float) -> Float)) => Float ->
(OpCodes,Float) -> Float
Note the (|$|) - fromInt 1 bit, hugs has clearly split up the minus and
one rather than treating it as a number.
(v) THIS IS A GET-AROUND:
Calculator> (-1) |$| ("take_away", -23)
22.0
This forces hugs to behave itself, however it's strange that (i) works and
not (iii) ...
I hope this information is useful!
The hugs version is run under SunOS 5.7
The following startup text should help:
__ __ __ __ ____ ___ _________________________________________
|| || || || || || ||__ Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__|| __|| Copyright (c) 1994-1999
||---|| ___|| World Wide Web: http://haskell.org/hugs
|| || Report bugs to: [EMAIL PROTECTED]
|| || Version: May 1999 _________________________________________
Haskell 98 mode: Restart with command line option -98 to enable extensions
Reading file "/opt/Hugs98/share/hugs/lib/Prelude.hs":
Hugs session for:
/opt/Hugs98/share/hugs/lib/Prelude.hs
Type :? for help
This is run under a Terminal window and would normally be run on Sun
systems.
Other than that, thanks for a brilliant programming language !
Please get back to me on the error if possible
Cheers
Thop Hesketh-Roberts
[EMAIL PROTECTED]
[EMAIL PROTECTED]