Newbie: How do I print/format?

Discussion of Common Lisp
Post Reply
Jesdisciple
Posts: 26
Joined: Thu Feb 04, 2010 5:56 pm

Newbie: How do I print/format?

Post by Jesdisciple » Thu Feb 04, 2010 6:07 pm

Current reference: http://www.gigamonkeys.com/book/lather- ... -repl.html
Installation guide: http://www.osix.net/modules/article/?id=912

The first link gives this hello-world program:

Code: Select all

(defun hello-world ()
  (format t "Hello, world!"))
Whether that statement is wrapped in a function or not, all it does is return NIL on my setup. Maybe it's because I'm using a different compiler (Clisp)?

The best I managed was to get FORMAT to return the given string (the same thing PRINT does):

Code: Select all

(format nil "Hello, world!"))
Calling this function only prints "G":

Code: Select all

defun hello-world ()
  (format nil "Hello, world!")
  (print "F")
  "G")
Did I miss something...? Thanks.
I'm off my grokker.
- Chris

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Newbie: How do I print/format?

Post by ramarren » Thu Feb 04, 2010 11:40 pm

Jesdisciple wrote:Whether that statement is wrapped in a function or not, all it does is return NIL on my setup.
In Common Lisp there are no statements, there are only expressions. Which means that everything has to return something (well, more or less), and usually a side-effecting function, like (format t ...) will return NIL. If you want to print something, why are you concerned with return value?

Usually code in Lisp is run from the REPL, which is Read-Eval-Print Loop, and it will print results of expressions which forms entered into return, but that is not relevant for printing in general. Anyway, on my Clisp:

Code: Select all

[1]> (defun hello-world ()
  (format t "Hello, world!"))
HELLO-WORLD
[2]> (hello-world)
Hello, world!
NIL
Where the line before last is the line printed by format, and the last one is the return value printed by REPL. What is you problem, exactly?

EDIT: Oh, I see the installation guide you are using also explains Emacs/Slime setup, which is good. In that setup the output from side effecting printing will go to *inferior-lisp* buffer by default. You can get all output in REPL buffer by putting in file ~/.swank.lisp

Code: Select all

(SETF SWANK:*GLOBALLY-REDIRECT-IO* T)
Case is not important.

EDIT 2: Also forgot to mention that, unless the situation improved recently, Common Lisp packages in distribution repositories are usually really old. I would recommend using clbuild to get them, starting with slime.

Jesdisciple
Posts: 26
Joined: Thu Feb 04, 2010 5:56 pm

Re: Newbie: How do I print/format?

Post by Jesdisciple » Sat Feb 06, 2010 2:07 pm

Ah, so there it is! Many thanks.

When I used the line you gave, it made *inferior-lisp* into the REPL rather than putting output in *slime-repl clisp*. Is this the intended effect? (It is certainly better than what I had, just not what I expected.)

I shall now write the expression "no statements in Lisp" 1000 times to the REPL.
I'm off my grokker.
- Chris

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Newbie: How do I print/format?

Post by ramarren » Sat Feb 06, 2010 2:34 pm

Jesdisciple wrote:When I used the line you gave, it made *inferior-lisp* into the REPL rather than putting output in *slime-repl clisp*. Is this the intended effect? (It is certainly better than what I had, just not what I expected.)
That is not what was intended, and not what happens on my system. It depends on the version of Slime you are using I think... at some point REPL was moved to optional modules, which means you need (slime-setup '(slime-repl)) in your .emacs, or even better (slime-setup '(slime-fancy)), rather than (slime-setup) alone, which configures the slime to not use any contribs. I have in fact (slime-setup '(slime-fancy slime-autodoc slime-asdf slime-banner slime-indentation)), but some of that is probably redundant.

hewih
Posts: 30
Joined: Tue Jan 19, 2010 9:36 am

Re: Newbie: How do I print/format?

Post by hewih » Sat Feb 06, 2010 2:54 pm

you might also have a look at the Lisp HyperSpec about format (have fun :))
or the Common Lisp Quick Reference once you know what is possible with format and print

Jesdisciple
Posts: 26
Joined: Thu Feb 04, 2010 5:56 pm

Re: Newbie: How do I print/format?

Post by Jesdisciple » Mon Feb 08, 2010 8:44 am

Apologies for not responding sooner. I thought I had auto-subscribe on.

Wow, you're right: Ubuntu's repo'd Slime is way out of date. I ran into problems with its implementation of the input functions earlier.

Oh, and sorry for not even mentioning the REPL in my OP. That the problem was related more to it than to clisp itself should have occurred to me.
I'm off my grokker.
- Chris

Post Reply