What is Expressiveness in a Computer Language

Greg Buchholz sleepingsquirrel at yahoo.com
Tue Jun 27 12:21:57 EDT 2006


Chris F Clark wrote:
> Very impressive.  It looks right to me and simple enough to
> understand.  I must find the time to learn a modern FP language.  Can
> you write a fold for this that prints the data as a binary tree of
> triples?  I have to believe it isn't that hard....

{- Refactoring this as a fold is left as an exercise to the reader -}

data Clark a b c = Nil | Cons a (Clark b c (a,a)) deriving Show

clark =
  (Cons 42 (Cons 3.14 (Cons "abc"
  (Cons (1,2) (Cons (1.2,3.4) (Cons ("foo","bar")
  (Cons ((9,8),(7,6)) (Cons ((0.1,0.2),(0.3,0.4))
                        (Cons (("w","x"),("y","z")) Nil)))))))))

main = print (toTree clark)

data Tree a = Empty | Node a (Tree a) (Tree a) deriving Show

toTree :: Clark a b c -> Tree (a,b,c)
toTree (Cons x (Cons y (Cons z rest)))
         = Node (x,y,z)
                (toTree (fst (lift_c rest)))
                (toTree (snd (lift_c rest)))
toTree _ = Empty

lift_c :: Clark (a,a) (b,b) (c,c) -> (Clark a b c, Clark a b c)
lift_c Nil = (Nil,Nil)
lift_c (Cons (x,y) rest) = (Cons x (fst (lift_c rest)),
                            Cons y (snd (lift_c rest)))




More information about the Python-list mailing list