0

After briefly talking about LISP in a past class, I have decided to jump in head first and try to learn CLISP (reading Seibel's PCL chpt 5). My question is in regards to writing a function that takes a set of lists as parameters. The first list is a series of indexes mapped to the second list. I want to pass a sequence of indexes and have it return the corresponding elements.

Here is the outline of my code so far. I wasn't sure if I could use nth and pass a list of arguments to it. I am not sure what the body-form should look like.

sys-info: CLISP 2.49 Win7

(defun get-elements('(nth (x y z) '(a b c)) )  
  "takes a list of arguments that returns corresponding elements from a list."
  (format t "The elements at index ~d are: ~%" x y z)
  ((maybe indexes go here)'(elements go here?)) 

The list (x y z) are the indexes and the data list (a b c) is some list of arbitrary elements. The evaluation is passed as data to the function get-elements. Am I even on the right track with this line of thinking?

Hints and pointers to relevant topics in LISP education are greatly appreciated.

postmortem: Upon re-examination of chpts 3-4, it would seem that PCL is a bit of a reach for a beginning programmer (at least for me). I can enter the code from the book, but I obviously don't have a deep understanding of the basic structure of the language. I will probably try a few gentler introductions to Lisp before undertaking PCL again.

cogito tute
  • 3
  • 1
  • 5
  • Can you give an example of what your code is supposed to do? – sds Nov 16 '17 at 16:57
  • There is a reason why a book is organized in chapters. Starting with chapter 5 of a book about a _new_ language is unlikely to be a good idea. Why don't you start with chapter 1? – sds Nov 16 '17 at 16:59
  • I should clarify that I have made it to chpt 5. After making the ripped CD db in chpt 3 I was hooked. – cogito tute Nov 16 '17 at 17:03
  • I would like the code to eventually do something like this (get-elements 0 1 0) -> (a b a) – cogito tute Nov 16 '17 at 17:05
  • If you did read the previous chapters, how come you have the quote in the lambda list of your function? – sds Nov 16 '17 at 17:21
  • Thanks for the hint, I will go back through 3-4. – cogito tute Nov 16 '17 at 17:29

1 Answers1

3

I am not quite sure if this is what you are asking about, but you might want to try:

(defun get-nth (index-list data-list)
  (mapcar (lambda (index)
            (nth index data-list))
          index-list))
(get-nth '(0 1 0 2 0 3) '(a b c d e f))
==> (A B A C A D)

Please take a look at

More gentle introductions to Lisp:

sds
  • 58,617
  • 29
  • 161
  • 278
  • Thank you, that is more than enough to get me started. fwiw, most of my self-generated code barfed in the repl. I will be more careful when posting code in the future. – cogito tute Nov 16 '17 at 17:32