This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Can anyone help me? [atomic-count list]

3 posts · 4351 views

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]

For example like this:
(defun atomic-count (list)
  (length (flatten list)))
where flatten is http://rosettacode.org/wiki/Flatten_a_list#Common_Lisp.
Another solution is:
(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). Temporary mirrors of aferomentioned: CLHS and a dark version.

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

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