[Haskell-cafe] fromInteger for Lists

John Dorsey haskell at colquitt.org
Fri May 1 19:06:15 EDT 2009


> There's nothing better than making a data type an instance of Num. In
> particular, fromInteger is a joy. But how about lists?

Do you mean something like Blargh below, only useful?


dorsey at elwood:~/src/scratch$ cat list.hs 

class Blargh f where
  fromList :: [a] -> f a

data Foo a = Foo [a] deriving (Show)
data Bar a = Bar [a] deriving (Show)

instance Blargh Foo where
  fromList = Foo

instance Blargh Bar where
  fromList l = Bar (reverse l)

dorsey at elwood:~/src/scratch$ ghci list.hs
GHCi, version 6.8.3: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( list.hs, interpreted )
Ok, modules loaded: Main.
*Main> fromList [1,2,3] :: Foo Int
Foo [1,2,3]
*Main> fromList [1,2,3] :: Bar Int
Bar [3,2,1]

More information about the Haskell-Cafe mailing list