Help with a simple LISP function in LispBox

Discussion of Common Lisp

Help with a simple LISP function in LispBox

Postby protosschick » Fri Jul 24, 2009 1:50 pm

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:

Code: Select all
(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.
protosschick
 
Posts: 2
Joined: Fri Jul 24, 2009 1:40 pm

Re: Help with a simple LISP function in LispBox

Postby findinglisp » Fri Jul 24, 2009 5:08 pm

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/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX

Re: Help with a simple LISP function in LispBox

Postby nuntius » Fri Jul 24, 2009 5:41 pm

Style issue: when you're testing a single condition, use IF, WHEN, or UNLESS
Code: Select all
(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?
User avatar
nuntius
 
Posts: 498
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: Help with a simple LISP function in LispBox

Postby gugamilare » Fri Jul 24, 2009 6:06 pm

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.
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Help with a simple LISP function in LispBox

Postby protosschick » Sun Jul 26, 2009 4:41 pm

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.
protosschick
 
Posts: 2
Joined: Fri Jul 24, 2009 1:40 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests