|
- [x:xs] is not the same as (x:xs)
- [x:xs] :: [[t]] but (x:xs) :: [t].
Somehow it is very easy to write [x:xs] in a pattern when
you really mean (x:xs).
The compiler is likely to report a (mystifying) type error
caused by this mistake at some other location in the program.
The slip is probably so easy because the list constructor, `:',
puts one in a list frame of mind.
It is almost worth doing a search for \[.*:.*\]
before compiling a program.
It is possible that [x:xs],
a list of one element which is itself a non-empty list,
is appropriate in some program but not often.
- Numbers
- Haskell has a sophisticated collection of
types - Int, Integer, Float, Double - and classes -
Num, Real, Fractional, Integral, RealFrac, Floating, RealFloat -
describing numbers and related concepts
(H98 section 6.4 p81).
Usually they work as expected but may sometimes trip
one who is familiar with other languages...
| e.g. 1 | e.g. 2 | e.g. 3 |
code |
n = 4
m = ceiling(n/2)
|
n = length [1,2,3,4]
m = ceiling(n/2)
|
n = length [1,2,3,4]
m = ceiling( (fromIntegral n)/2 )
|
result |
OK, output:
4.0
2
| Error:
No instance for (RealFrac Int)
arising from use of `ceiling' at file.hs:5
In the definition of `m': ceiling(n/2)
(- ghc 9/2002)
| OK, output:
4
2
|
why |
4 ~ fromInteger(4) :: Float,
hence 4.0
|
n :: Int
(/) :: (Num a) => a->a->a
but ¬RealFrac Int
|
NB. 4 not 4.0
|
The error may be "discovered" when a function,
which itself compiles, is used in a new context.
The compiler may detect an error that is distant from the true cause.
|
Haskell:
(:) | cons |
[x1,...] | list |
[ ] | list |
(++) | append |
\ | λ :-) |
:: | has type |
Compared
|
|
|