Re: a clisp problem



Thank you very much, Pascal.

In the meantime, I found you older post about this problem. The
solusion is good. It took me some time to understand.

best regards,
keyboard

Pascal Bourguignon schrieb:

> "keyboard" <wen-sheng.xie@xxxxxxxxxxx> writes:
> > I'm new to lisp, using GNU Clisp.
> >
> > I have a problem when I do some Lisp programming training.
> >
> > For example, for the list (a (b1 b2) c (d1 d2) (e1 e2 e3) f), I need a
> > function to return all the possible combinations like:
> > ((a b1 c d1 e1 f)
> > (a b1 c d1 e2 f)
> > (a b1 c d1 e3 f)
> > (a b1 c d2 e1 f)
> > (a b1 c d2 e2 f)
> > (a b1 c d2 e3 f)
> > (a b2 c d1 e1 f)
> > (a b2 c d1 e2 f)
> > (a b2 c d1 e3 f)
> > (a b2 c d2 e1 f)
> > (a b2 c d2 e2 f)
> > (a b2 c d2 e3 f))
> >
> > My tries trended to be unsuccessful. It's not homework. Can highhand
> > help?
>
> I would normalize first the list:
>
> (a (b1 b2) c (d1 d2) (e1 e2 e3) f)
> --> ((a) (b1 b2) (c) (d1 d2) (e1 e2 e3) (f))
>
>
> Then you can develop easily a recursive algorithm:
>
> The base case is:
> - the combinations of () are (())
>
> The recursive formula will be of the form:
>
> - when you have a list
> (combinations list) = (f (first list) (combinations (rest list)))
>
> You need to write the function f.
>
> For example, when list = ((a b c))
> (first list) = (a b c)
> (rest list) = ()
> From the base case:
> (combinations (rest list)) = (())
>
> So you need to call: (f '(a b c) '(()))
> and obtain: ((a) (b) (c))
>
>
> Take as second example: list = ((a b c) (1 2 3))
> and the definition of f should become obvious.
>
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> "Our users will know fear and cower before our software! Ship it!
> Ship it and let them flee like the dogs they are!"

.