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
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® account now.
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners