[Haskell-cafe] Type inference; always

Derek Elkins derek.a.elkins at gmail.com
Sun May 18 13:51:18 EDT 2008


On Sun, 2008-05-18 at 21:16 +0400, Bulat Ziganshin wrote:
> Hello Derek,
> 
> Sunday, May 18, 2008, 9:10:38 PM, you wrote:
> > This is incorrect.  There are two (other) situations where you need type
> > annotations in Haskell 98.  One situation is when you use polymorphic
> > recursion, but that is pretty rare unless you are writing nested data
> > types
> 
> can you give examples?


-- untested
-- all type annotations are necessary
-- A nested data type example
data PerfectTree a = Leaf a | Succ (PerfectTree (a,a))

size :: PerfectTree a -> Int
size (Leaf _) = 1
size (Succ t) = 2 * size t

-- a toy example without nested data types
f :: Show a => Int -> a -> String
f 0 x = show x
f n x = f (n-1) (x,x)

-- a less toy example
bitReverse :: [a] -> [a]
bitReverse [x] = [x]
bitReverse xs  = uncurry (++) . unzip . bitReverse . pairUp $ xs
    where pairUp       [] = []
          pairUp (x:y:xs) = (x,y):pairUp xs



More information about the Haskell-Cafe mailing list