
The line "isOneOnly oneOnly = True" doesn't do what you expect here.
Basically, it says there are no constraints on the input, and it binds
whatever input it gets to a new *local* variable named oneOnly. Thus, it
always matches that line of the function, and always returns true.
The problem here is that [1] is a value, not a type that you can match on.
If you want to make sure the value is [1], you can do it one of these two
ways:
isOneOnly x = x == [1]
or
isOneOnly x | x == [1] = True
isOneOnly x | otherwise = False
Now at this point you may be wondering how functions like this work:
factorial 1 = 1
factorial n = n * factorial (n-1)
I just said that you can't match against values, but against types. What
gives? Well, in fact, haskell matches against integers as types of sort, of
the structure Succ Int. That is, it treats integers like they were encoded
with the following type:
data Nat = Zero | Succ Nat
Now you can see how it could pattern match against integers, just like it
would against other types with data constructors. Anyway, I"m sure this is
far more information than you wanted to know.
On Tue, Dec 23, 2008 at 7:45 PM, Raeck Zhao
hi, good ... morning : ) I am just confused by the following code
oneOnly :: [Int] oneOnly = [1] isOneOnly :: [Int] -> Bool isOneOnly oneOnly = True isOneOnly tester = False
what I want to do is to define a 'type' oneOnly as [1] and use it on the pattern matching in function isOneOnly. But it does not work as I expect:
When I type
isOneOnly [1]
it will be True which is the result I expect but for
is OneOnly [1,2]
the result keeps True, it seems the second pattern has been ignored, I think I try to achieve the purpose in a wrong way, any suggestion?
Thanks and Merry Christmas
Best wishes, Raeck
------------------------------ Send e-mail anywhere. No map, no compass. Get your Hotmail(R) account now.http://windowslive.com/oneline/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_anywher...
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners