Common Mistakes (doh!)

LA home
 FP,  λ
 Logic,  π

[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.
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:
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:
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.
window on the wide world:

Computer Science Education Week

free op. sys.
free office suite,
ver 3.4+

~ free photoshop
web browser
like it says!

(:) cons
[x1,...] list
[ ]list
(++) append
\ λ :-)
:: has type

© L. Allison   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Saturday, 19-Apr-2014 04:12:33 EST.