69 lines
2.2 KiB
Common Lisp
69 lines
2.2 KiB
Common Lisp
(defun say-hello ()
|
|
(print "Please type your name:")
|
|
(let ((name (read)))
|
|
(print "Nice to meet you, ")
|
|
(print name)))
|
|
|
|
;; Type name in quotes in order for it to be a string and not a symbol
|
|
;; When running the function
|
|
|
|
;; First we print a message asking users for a name
|
|
;; Next we define a local variable called "name" which is set
|
|
;; to the value returned by (read)
|
|
|
|
;; (read) will cause lisp to wait for the user to type in something
|
|
;; Only after the user has typed something and pressed ENTER will
|
|
;; the variable name be set to the result
|
|
|
|
;; Now that we have the user's name, we print a personalized greeting
|
|
|
|
;; The print command leaves in the quotes when printing to the screen.
|
|
|
|
;; It is recomended to use (print) or (read) if we can get away with it
|
|
;; in order to save doing extra work. (I still like (format))
|
|
|
|
;; Basically, (read) and (print) simply take in and spit out things
|
|
;; In a computer understood way. Hence the quotes.
|
|
|
|
;;Let's do something with numbers
|
|
|
|
(defun add-five ()
|
|
(print "please enter a number:")
|
|
(let ((num (read)))
|
|
(print "When I add five I get")
|
|
(print (+ num 5 ))))
|
|
|
|
;; We can use literal characters by putting #\ in front of it
|
|
;; So we can do #\a to get "a"
|
|
|
|
;; There are also special characters used this way
|
|
;; #\newline #\tab #\space .. Should be self-explaintory
|
|
|
|
;; To avoid getting quotes back from print and prin1
|
|
;; we can use (princ)
|
|
|
|
(progn (princ "This sentence will be interrupted")
|
|
(princ #\newline)
|
|
(princ "By an annoying newline character."))
|
|
;; >> This sentence will be interrupted
|
|
;; >> by an annoying newline character.
|
|
|
|
;; This is posibly a better methond than bothering with (format)
|
|
|
|
;; Now let's create a say-hello function that doesn't suck
|
|
(defun say-hello2 ()
|
|
(princ "Please type your name:")
|
|
(let ((name (read-line)))
|
|
(princ "Nice to meet you, ")
|
|
(princ name)))
|
|
|
|
;; This is similar to our original, but now it doesn't
|
|
;; print quotes around the string.
|
|
;; It just takes everything the user enters as one big string
|
|
|
|
;; back to the game
|
|
;; So far we've been able to do everything in our game from the lisp repl
|
|
;; this is awesome for prototyping, but it's time to make a real interface
|
|
|
|
;; (see the new wizard file)
|