Page 1 of 1

Can anyone help me? [atomic-count list]

Posted: Tue Jul 03, 2012 12:57 pm
by geassking
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....

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

Posted: Wed Jul 04, 2012 2:14 am
by Goheeca
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.

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

Posted: Wed Jul 04, 2012 12:15 pm
by sylwester
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?