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.

Help with a simple LISP function in LispBox

5 posts · 7225 views

Greetings,

I'm a beginner in Lisp and I have this small function which is supposed to print out the head of every list (including lists within lists).

However, when I run the program, it only prints out the very first head, like so:

CL-USER> (first y)

New Y: (1.(2.3))

1


Here is what my program looks like:
(defun first (aList)
  (cond
    ((not(atom aList))
     (format t "~a" (car aList))
     (cond
       ((not(atom(car aList)))
        (first(car aList)))
       )
     )
    )
  (dolist(item(cdr aList))
    (cond
      ((not(atom item))(first item))
      )
    )
  )

( defvar x ( list 'a 'b 'c 'd ) )
( defvar y ( list ( list 'a 'b ) 'c 'd) )
( defvar z ( list 'a ( list ( list 'b 'c) 'd ) 'e ( list 'f 'g ) ) )

( first x )
( first y )
( first x )
Am I doing something obviously major and wrong? I just installed LispBox today and haven't added anything special to it.

Any tips/advice are welcomed with enormous gratitude.

Re: Help with a simple LISP function in LispBox

Moved this to Common Lisp forum as it appears the question revolves around that, rather than Emacs Lisp. Please categorize your posts correctly.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Re: Help with a simple LISP function in LispBox

Style issue: when you're testing a single condition, use IF, WHEN, or UNLESS
(if (test something)
  (do-if-true)
  (do-if-false))
(when (test something)
  (do-if-true))
(unless (test something)
  (do-if-false))
As to your question, there's already a built-in function named FIRST. Did you receive an error when when defining your function? Are you sure you're calling the right function?

Re: Help with a simple LISP function in LispBox

Some other suggestions.

The most important thing: rename your function to print-first or something else, since, as already said, the function first is standard, and redefining a standard function might work or might not work (in this case, it didn't). It would be more readable if you cleaned up your code a little bit. For instance, don't put a single ')' in a new line because it will make your function larger. In Lisp, smaller functions are easier to read. Instead, put the parens all together after the last expression and use some editor that does auto-indentation for Lisp. You may feel a little insecure to do that at first but, if you let the editor keep the lines indented, you won't even notice the parens.

The last thing, it would be interesting to change "~a" with "~a~%" or else it will print a lot a symbols in a single line, making it hard to read.

If you make these adjustments, your function will look a lot better, and it also will work, I tested it here.

Re: Help with a simple LISP function in LispBox

RE: findinglisp- Okay, I'm sorry about that.

RE: nuntius and gugamilare-

Thank you for the help. I didn't realize first was a built-in function and following with your advice got my program to work. Now I'm just working on getting a new function to print the tail of each list, but it should be easy.