[Haskell-cafe] [Wadler 89] Philip Wadler. "Theorems for free!": Can't get the same results in Haskell

Stayvoid stayvoid at gmail.com
Fri Aug 10 02:52:54 CEST 2012


Hi there,

I was reading "Real World Haskell" and found this paper. [1]

On the first page of the paper there is an example:

inc* (odds_Int [1,2,3])
= [2,4]
/= [4]
= odds_Int (inc* [1,2,3])

I tried to implement it in Haskell:
(I'm a newbie. I guess it's possible to write a better version.)

module Param where
import Prelude

odds :: [Int] -> [Int]
odds [] = []
odds [x] = if odd x
           then [x]
           else []
odds (x:xs) = if odds [x] == []
              then odds xs
              else [x] ++ odds xs

inc :: [Int] -> [Int]
inc [] = error "Empty list"
inc [x] = [succ x]
inc (x:xs) = inc [x] ++ inc xs

Looks fine:

*Param> odds [1,2,3]
[1,3]
*Param> inc [1,2,3]
[2,3,4]

But my results differ from the paper's:

*Param> inc (odds [1,2,3])
[2,4]
*Param> odds (inc [1,2,3])
[3]

I doubt that there is an error in the paper. So it seems that
something is wrong with my code. But I can't find the error. Could you
help me?
(Or there is no error at all and I misunderstood something.)

[1] http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf

Thanks



More information about the Haskell-Cafe mailing list