LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-28-2005, 01:21 AM   #1
keyboard
LQ Newbie
 
Registered: Dec 2004
Location: China
Distribution: debian
Posts: 24

Rep: Reputation: 15
A Clisp probelm


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. Is it too easy to ask this here? If so, where is the forum for people like me?


best regards,
keyboard
 
Old 11-28-2005, 03:20 AM   #2
keyboard
LQ Newbie
 
Registered: Dec 2004
Location: China
Distribution: debian
Posts: 24

Original Poster
Rep: Reputation: 15
seems not easy.

up
 
Old 11-28-2005, 05:22 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
homework?

first I would flatten the list, then do the combinations.
 
Old 11-28-2005, 05:25 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
some good lisp references:

http://www.lispworks.com/documentati...ront/index.htm
http://www.gigamonkeys.com/book/
http://psg.com/~dlamkins/sl/cover.html
 
Old 11-28-2005, 08:17 PM   #5
keyboard
LQ Newbie
 
Registered: Dec 2004
Location: China
Distribution: debian
Posts: 24

Original Poster
Rep: Reputation: 15
Thank you, billy.

This is not so easy as you thought. It's too hard to be a homework.

After you flatten, you lost some list structure information to do the combination.
I tried iterate and recursive, too tedious to finish. Maybe I am on the wrong way.

The refers are good.
 
Old 11-29-2005, 05:44 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
Quote:
After you flatten, you lost some list structure information to do the combination.
true but your solutions seem to be flat so should it not be okay?
it's easier if you keep the inner conses.

You are right, it's not as easy as it first looks.
I love lisp
 
Old 11-29-2005, 09:03 PM   #7
keyboard
LQ Newbie
 
Registered: Dec 2004
Location: China
Distribution: debian
Posts: 24

Original Poster
Rep: Reputation: 15
got this from comp.lang.lisp

(defun combine (lst)
(if (endp lst)
(list '())
(loop with crest = (combine (rest lst))
for item in (first lst)
nconc (loop for items in crest
collect (cons item items)))))
 
Old 12-01-2005, 07:15 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
I reckon flatten the list first then combinate:

Code:
(defun flatten (l) 

    (cond
       ((null l) ())
       ((atom l) l)
       ((atom (car l)) (cons (car l) (flatten (cdr l))))
       ((listp (car l)) (concatenate 'list (flatten (car l)) (flatten (cdr l))))
))
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wusb11 probelm RockyB Linux - Wireless Networking 3 01-20-2005 02:58 AM
SML and Clisp turtorial Haraldsh Linux - General 0 01-19-2005 12:08 PM
Probelm In Network pradi_net Linux - Networking 7 05-05-2004 04:05 AM
Internet Probelm Fonke0412 Linux - Networking 1 03-07-2004 12:26 PM
where do I learn AI with CLISP/LISP ? pepo Programming 4 01-25-2003 06:42 AM


All times are GMT -5. The time now is 02:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration