views:

54

answers:

3

Hi,

I'm just starting off with Lisp and need some help. This is technically homework, but I gave it a try and am getting somewhat what I wanted:

(defun speed (kmp)  
  (cond ((> kmp 100) "Fast")    
        ((< kmp  40) "Slow") 
        (t           "Average")))

However, if I run the program it displays "Average" instead of just Average (without the quotes).

How can I get it to display the string without quotes?

+2  A: 

The read-eval-print loop displays the return value of your function, which is one of the strings in a cond branch. Strings are printed readably by surrounding them with double-quotes.

You could use (write-string (speed 42)). Don't worry that it also shows the string in double-quotes - that's the return value of write-string, displayed after the quoteless output.

Xach
+1  A: 

You can also use symbols instead of strings:

(defun speed (kmp)  
  (cond ((> kmp 100) 'fast)    
        ((< kmp  40) 'slow) 
        (t           'average)))

Symbols are uppercased by default, so internally fast is then FAST.

You can write any symbol in any case and with any characters using escaping with vertical bars:

|The speeed is very fast!|

Above is a valid symbol in Common Lisp and is stored internally just as you write it with case preserved.

Rainer Joswig
+1  A: 

You can use symbols instead of strings. But keep in mind that symbols will be converted to uppercase:

> 'Average
AVERAGE 

If you care about case or want to embed spaces, use format:

 > (format t "Average")
 Average
Vijay Mathew