INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

A procedure to extract atoms from a list

A procedure to extract atoms from a list

(OP)
Hello. I need to right a recursive procedure that receives a list (could be nested) or an atom as input and returns a new list in which only the atoms of the input data are extracted. Further, if there are repeating atoms only one of them must be listed. It should look something like this:

(extract-atoms (a b (c d) (e f (c d b))))

: (a b c d e f)

I really need that procedure for a model I am trying to program. Any help will be appreciated.

RE: A procedure to extract atoms from a list

For example something like this:
atoms.lsp

CODE

;;; *** Functions ***
(defun new_line ()
  (format t "~%"))

(defun is-element-of-list (element input-list)
(cond ((member element input-list) t)
      (t nil)))

(defun select-atoms (input-list output-list)
  ; copy output-list into atom-list
  (setq atom-list (copy-list output-list))
  ;; 1. process first element of the list
  ; if the first element is an atom
  (cond ((atom (car input-list))
         ; if the first element is not in the atom-list
         (cond ((not (is-element-of-list (car input-list) atom-list))
           ; add atom-element to the atom-list
           (setq atom-list (cons (car input-list) atom-list))
           ; uncoment to see the results
           ;(print atom-list)
         )))
        ; else if the first element is not an atom
        (t (select-atoms (car input-list) atom-list)))
  ;; 2. process rest of list
  (cond ((not (null (cdr input-list)))
         (select-atoms (cdr input-list) atom-list )))
  ;; at end return atom-list
  (reverse atom-list))

(defun extract-atoms (input-list)
  ; call select-atoms with empty output-list
  (select-atoms input-list '()))


;;; *** Main program ***
(setq input_list '(a b (c d) (e f (c d b))))
(format t "~%input-list = ~A" input_list)
(new_line)
(setq atom_list (extract-atoms input_list))
(format t "~%atom-list = ~A" atom_list)
Output:

CODE

C:\Users\Roman\Work>clisp atoms.lsp

input-list = (A B (C D) (E F (C D B)))

atom-list = (A B C D E F)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close