practice/lisp/ansi_lisp/ch2/dunno_yet.lisp

119 lines
3.2 KiB
Common Lisp
Raw Permalink Normal View History

2017-06-23 02:16:42 -07:00
;example of printing proper
;t indicates that the output is to be sent to the default place
; normally this will be toplevel
; ~% indcates newline
; it's like printf
;(format t "~A plus ~A equals ~A.~%" 2 3 (+ 2 3))
;user-input
(defun askem (string)
(format t "~A" string)
(read))
;example
;(format t "You wrote: ~A ~%" (askem "What is you????? "))
;user-input plus variables
(defun ask-number ()
(format t "Please enter a number. ")
;;;create a variable "val" to store read (first two arguments of let)
(let ((val (read)))
;;;;Now after asigning values in let, we can do expressions
;;;;Note that the variables are local to the let block
;;;;Test if val is number
(if (numberp val)
val
(ask-number))))
;example
;(format t "~A~%" (ask-number))
;global variables
;The asterisks are simply a standard to show that the variable is global
(defparameter *glob* 99)
;global constants can be declared as such
;They don't need special names because if they share the name of a variable
;it'll throw an error
(defconstant limit (+ *glob* 1))
;We can check if a symbol is the name of a global variable/constant like this
;(format t "~A~%" (boundp '*glob*))
;(format t "~A~%" (boundp 'limit))
;The most general assignment operator is setf
(setf *glob* 98)
(format t "~A~%" (let ((n 10))
(setf n 2)
n))
;If the first arg to setf is a symbol that is not a local variable, it is
;taken to be global and thus creates one if it didn't exist already
;Note that this is bad practice because it pisses off my linter
;also, implicit declarations are weird.
;(setf x (list 'a 'b 'c))
;(format t "~A~%" x)
; we could also do something like (assuming the above code has also happened)
;We can use an expression instead of a variable. In these cases the second
;argument is inserted in the place refered by the first arg
;So here:
;(setf (car x) 'n)
;We would be replacing the A in (A B C) with an N
;You can also give any even about of arguments to setf... so...:
;(setf a b
; c d
; e f)
;Would be equivalnt to doing three seperate setf statements
;print out the square of the integer from start to end
;do works as (variable inital update)
;So here we have the first arg:
;;;i is set to start and then updates by 1
;The next argumen to (do) is when the test should end and
;;;what happens after end
;The third and last argument is what's actually happening (the format list)
(defun show-squares (start end)
(do ((i start (+ i 1)))
((> i end) 'done)
(format t "~A ~A~%" i (* i i))))
;Recursive version
;progn can take any number of expressions, it evaluates them in order, and
;returns the value of the last
(defun show-square-rec (i end)
(if (> i end)
'done
(progn
(format t "~A ~A~%" i (* i i))
(show-squares (+ i 1) end))))
;function that iterates through a list
;dolist takes an argument as (variable expression) followed
;by a body of expressions
(defun our-length (lst)
(let ((len 0))
(dolist (obj lst)
(setf len (+ len 1)))
len))
;rec version
(defun our-length-rec (lst)
(if (null lst)
0
(+ (our-length (cdr lst)) 1)))
;define function tha takes two arguments and returns the greater
(defun is-greater (arg1 arg2)
(if (> arg1 arg2)
(format t "~A~%" arg1)
(format t "~A~%" arg2)))