[Haskell-cafe] Re: [Haskell] Trying to get a Composite design
pattern to work
ndmitchell at gmail.com
Mon Mar 13 18:25:07 EST 2006
> How's this?
What about ++, in Haskell thats just an ordinary function, yet you are
using the library one in this case.
The other thing is that the first definition of my_concat_map is
entirely redundant, the second one handles both cases anyway.
Also, for completeness, you might be interested to find that in some
compilers the list is actually defined as:
data  a =  | (:) a ( a)
This isn't legal Haskell 98, but nhc and Yhc both define a list
similarly to 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
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe