Please help a newbie with 3 (simple) problems

Discussion of Common Lisp

Please help a newbie with 3 (simple) problems

Postby DaJackal » Sat Dec 12, 2009 6:51 am

Hi!

I'm a newbie in Lisp, and i have some problems with some simple code.

1) My first problem is like this: I have a tree in Lisp like with this representation: (A 2 B 0 C 2 D 0 E 0) , which would be equivalent with (A (B) (C (D) (E))). I need to know how could I obtain the Postorder Traversal Sequence for the first representation. I figured out how to do it for the second representation of the tree, but I can't do it for the first one. Please help me if you can.

At this problem, I tried something, but it didn't work:

(defun post(l)
(cond
((null l) nil)
(t(cons(post(caddr l))(car l))))
)
)

For the other 2 problems, i have to make non-recursive functions, I have to resolve it iteratively (I'm not sure if this is the right word):

2) I have 2 lists, and I need to make a function that makes an association list between those 2 lists. For eg, (A B C) (X Y Z) --> ((A.X) (B.Y) (C.Z))

3) a) I have a list with atoms and I want to make another list that shows for every atom, how many times it appears in the list, something like this:
(A B A B A C A) --> ((A 4) (B 2) (C 1))
b) A function that counts the atoms from a list, something like: (A (B C) D E) --> 3 or (A B C) --> 3

Thank you very much in advance, and please help me if you can, it's very important to me.

Have a nice day!
DaJackal
 

Re: Please help a newbie with 3 (simple) problems

Postby Ramarren » Sun Dec 13, 2009 12:29 pm

DaJackal wrote:I have a tree in Lisp like with this representation: (A 2 B 0 C 2 D 0 E 0)


A single example is not enough to know how representation works. More importantly, once you formally specify a representation, implementing simple operations on it becomes trivial. I suggest you try doing that.

DaJackal wrote:2) I have 2 lists, and I need to make a function that makes an association list between those 2 lists. For eg, (A B C) (X Y Z) --> ((A.X) (B.Y) (C.Z))


Common Lisp already have such function: PAIRLIS. Therefore, implementation you need would depend on what constructs exactly are allowed, assuming this is homework, which seems a pretty safe assumption.

DaJackal wrote:a) I have a list with atoms and I want to make another list that shows for every atom, how many times it appears in the list, something like this:
(A B A B A C A) --> ((A 4) (B 2) (C 1))


Either a combination of REMOVE-DUPLICATES, COUNT and MAPCAR, or much more simply a DOLIST and a hashtable will work, but again this depends on which concepts are allowed.

DaJackal wrote:b) A function that counts the atoms from a list, something like: (A (B C) D E) --> 3 or (A B C) --> 3

(count-if #'atom ...) but I am repeating myself.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron