Reputation: 19
I am trying to write a Lisp function to return a list of characters (no repeats) from a list (with ints, characters, etc). I'm still a beginner with Lisp and am having trouble starting. Our prof mentioned using atom but I can't figure out what she meant. Here is the question:
"Write a lisp function that accepts a list as the input argument (the list is mixed up integers, decimals, characters and nested lists) and creates a list including all the characters in the original list without any duplication. Sample program output is shown below:
Upvotes: 1
Views: 879
Reputation: 51501
What your assignment calls “characters” are actually symbols with a name of length 1. It seems that you can just mentally replace the word “characters” with “symbols” and work with this.
An atom is anything that is not a cons—any non-empty list consists of a chain of conses. For example, symbols, numbers, strings, and nil
are atoms.
A cons (actually a cons cell) is a simple datastructure that can hold two things. In a list, the first thing of each cons is some list element, and the second either a pointer to the next cons or nil. You can also have lists as list elements; then also the first thing would be a pointer to a list. This would then formally be a tree. The accessor function for the first thing of a cons is called car
or first
, the accessor function for the other thing is called cdr
or rest
. Car
and cdr
are a bit archaic, and mainly used when you see the cons cell as a tree node, while first
and rest
are more modern, and mainly used when you see the cons cell as a list chain link.
You can test whether a thing is an atom with the function atom
. If it is not an atom, it is a list with at least one element.
Your assignment has a few parts:
One useful idiom is to use push
or pushnew
, which put new elements at the front of the list, and at the end reverse
it.
Upvotes: 2