Re: [Haskell] Trying to get a Composite design pattern to work

2006-03-13 Thread ajb
G'day all.

Quoting Asfand Yar Qazi <[EMAIL PROTECTED]>:

> Actually, I'm trying to avoid library functions, so I can learn the
> language and the functional way of thinking.  How would one implement
> the concatMap function?

See if you can work how how this one works.  No library functions, apart
from function composition.

statesList :: StateNode a -> [a]
statesList s
  = statesList' s []
  where
statesList' (State x) = (x:)
statesList' (CompositeState xs) = statesLists xs

statesLists [] = id
statesLists (x:xs) = statesList' x . statesLists xs

Cheers,
Andrew Bromage
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] Trying to get a Composite design pattern to work

2006-03-13 Thread Asfand Yar Qazi
On 3/13/06, Greg Buchholz <[EMAIL PROTECTED]> wrote:
> Asfand Yar Qazi wrote:
> > I'm trying to implement hierarchical states in Haskell, following on from my
> > work at doing them in C++.
> >
> > Here's what I've got so far:
> >
> > data StateNode a= CompositeState [ StateNode a ] | State a
> > stateslist :: StateNode a -> [a]
> > stateslist(State x) = [x]
> > stateslist(CompositeState xs) = {- return list of objects of type a -}
> >
> > The following give errors (as they should)
> > -- stateslist(CompositeState xs) = [ stateslist(x) | x <- xs ]
> > -- stateslist(CompositeState xs) = map stateslist xs
> >
> > You see what I'm trying to do?  This is how I want it to behave:
> >
> > sm1 = CompositeState [ State 1, State 2, State 3 ]
> > stateslist(sm1)
> >   => [1, 2, 3]
>
> Maybe...
>
> stateslist :: StateNode a -> [a]
> stateslist (State x) = [x]
> stateslist (CompositeState xs) = concatMap stateslist xs
>
> Greg Buchholz

Actually, I'm trying to avoid library functions, so I can learn the
language and the functional way of thinking.  How would one implement
the concatMap function?

Thanks
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] Trying to get a Composite design pattern to work

2006-03-13 Thread Greg Buchholz
Asfand Yar Qazi wrote:
> I'm trying to implement hierarchical states in Haskell, following on from my
> work at doing them in C++.
> 
> Here's what I've got so far:
> 
> data StateNode a= CompositeState [ StateNode a ] | State a
> stateslist :: StateNode a -> [a]
> stateslist(State x) = [x]
> stateslist(CompositeState xs) = {- return list of objects of type a -}
> 
> The following give errors (as they should)
> -- stateslist(CompositeState xs) = [ stateslist(x) | x <- xs ]
> -- stateslist(CompositeState xs) = map stateslist xs
> 
> You see what I'm trying to do?  This is how I want it to behave:
> 
> sm1 = CompositeState [ State 1, State 2, State 3 ]
> stateslist(sm1)
>   => [1, 2, 3]

Maybe...

stateslist :: StateNode a -> [a]
stateslist (State x) = [x]
stateslist (CompositeState xs) = concatMap stateslist xs

Greg Buchholz
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


[Haskell] Trying to get a Composite design pattern to work

2006-03-13 Thread Asfand Yar Qazi
I'm trying to implement hierarchical states in Haskell, following on from my
work at doing them in C++.

Here's what I've got so far:

data StateNode a= CompositeState [ StateNode a ] | State a
stateslist :: StateNode a -> [a]
stateslist(State x) = [x]
stateslist(CompositeState xs) = {- return list of objects of type a -}

The following give errors (as they should)
-- stateslist(CompositeState xs) = [ stateslist(x) | x <- xs ]
-- stateslist(CompositeState xs) = map stateslist xs

You see what I'm trying to do?  This is how I want it to behave:

sm1 = CompositeState [ State 1, State 2, State 3 ]
stateslist(sm1)
=> [1, 2, 3]

How can I get this to work?  My mind has just gone all wobbly!

Thanks
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell