Common Mistakes (doh!)

LA home
Computing
 Algorithms
 Bioinformatics
 FP,  λ
 Logic,  π
 MML
 Prog.Langs

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

Computer Science Education Week

Linux
 Ubuntu
free op. sys.
OpenOffice
free office suite,
ver 3.4+

The GIMP
~ free photoshop
Firefox
web browser
FlashBlock
like it says!

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

© L. Allison   http://www.allisons.org/ll/   (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 Friday, 25-Apr-2014 15:57:55 EST.