Can anyone help me? [atomic-count list]

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.

Can anyone help me? [atomic-count list]

Postby geassking » Tue Jul 03, 2012 12:57 pm

Count atomic number program for example

(atomic-count '(c h 2 o 2))
return 5

(atomic-count '((c h 2) 3 o 2)
return 11


(atomic-count '(c 2 h 2 (o 2) 2))
return 8

T^T
help please....
thx so much....
geassking
 
Posts: 1
Joined: Tue Jul 03, 2012 12:22 pm

Re: Can anyone help me? [atomic-count list]

Postby Goheeca » Wed Jul 04, 2012 2:14 am

For example like this:
Code: Select all
(defun atomic-count (list)
  (length (flatten list)))

where flatten is http://rosettacode.org/wiki/Flatten_a_list#Common_Lisp.
Another solution is:
Code: Select all
(defun atomic-count (list)
  (loop for item in list sum
       (cond ((and (listp item) (not (null item))) (atomic-count item))
        (t 1))))


If it's really homework then I trust you that you exert some effort.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 202
Joined: Thu May 10, 2012 12:54 pm

Re: Can anyone help me? [atomic-count list]

Postby sylwester » Wed Jul 04, 2012 12:15 pm

geassking wrote:Count atomic number program for example

(atomic-count '(c h 2 o 2))
return 5

(atomic-count '((c h 2) 3 o 2)
return 11


(atomic-count '(c 2 h 2 (o 2) 2))
return 8


As far as I ca tell you really need magic because I cannot see the relationship between the arguments supplied and the returned number. Sometimes the number is added as number instead of counted as an atom but when?? Are you sure your examples are accurate?
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 83
Joined: Mon Jul 11, 2011 2:53 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 3 guests

cron