[Haskell-cafe] flatten a nested list

pphetra pphetra at hotmail.com
Fri Dec 29 02:56:58 EST 2006


I would like to write a program that can do something like this.
 
;; lisp syntax
* (my-flatten '(1 (2 (3 4) 5)))
(1 2 3 4 5)

I end up like this.

data Store a = E a | S [Store a]
             deriving (Show)

flat :: [Store a] -> [a]
flat [] = []
flat ((E x):xs) = [x] ++ flat xs
flat ((S x):xs) = flat x ++ flat xs

so
*Main> flat [E 1, S[E 2, S[E 3, E 4], E 5]]
[1,2,3,4,5]

Compare to a Lisp solution, It 's not looking good.
Any suggestion.

Thanks,
PPhetra
-- 
View this message in context: http://www.nabble.com/flatten-a-nested-list-tf2893713.html#a8084726
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list