[Haskell-cafe] Re: [Haskell] Trying to get a Composite design
pattern to work
Asfand Yar Qazi
ayqazi at gmail.com
Mon Mar 13 18:16:26 EST 2006
On 3/13/06, Jared Updike <jupdike at gmail.com> wrote:
>
> If you want a place to start, I would challenge you to expand the
> concatMap definition above with the definitions of concat and map, and
> then plug in the definition of foldr and see if you can make your own
> concatMap function. Maybe that will help you understand things better.
MUHAHAHAHA I AM TEH HAXXELL GENEUS. Ahem...
How's this?
myhead :: [a] -> a
myhead (x:xs) = x
mytail :: [a] -> [a]
mytail (x:xs) = xs
mymap :: (a -> b) -> [a] -> [b]
mymap fn [] = []
mymap fn (x:xs) = fn x : mymap fn xs
myconcat :: [[a]] -> [a]
myconcat (x:xs) = x ++ myconcat xs
myconcat [] = []
-- For each a in [a], produce a [b] in another list, then concat the
-- list.
my_concat_map :: (a -> [b]) -> [a] -> [b]
my_concat_map fn [] = []
my_concat_map fn xs = myconcat (mymap fn xs)
stateslist :: StateNode a -> [a]
stateslist(State x) = [x]
stateslist (CompositeState xs) = my_concat_map stateslist xs
More information about the Haskell-Cafe
mailing list