Added lisp along with some study work
This commit is contained in:
118
lisp/ansi_lisp/ch2/dunno_yet.lisp
Normal file
118
lisp/ansi_lisp/ch2/dunno_yet.lisp
Normal file
@@ -0,0 +1,118 @@
|
||||
;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)))
|
||||
|
||||
26
lisp/ansi_lisp/ch2/problem_8/print_dots.lisp
Normal file
26
lisp/ansi_lisp/ch2/problem_8/print_dots.lisp
Normal file
@@ -0,0 +1,26 @@
|
||||
;; Take a positive int and print that many dots
|
||||
|
||||
;; repetition
|
||||
(defun print-dots(num-of-dots)
|
||||
(do ((i 0 (+ i 1)))
|
||||
((= i num-of-dots) 'done)
|
||||
(format t ". ")))
|
||||
;; recursion
|
||||
(defun print-dots-rec(num-of-dots)
|
||||
;plusp checks if it's a positivie integer above 0.0
|
||||
(if (plusp num-of-dots)
|
||||
(progn
|
||||
(format t ". ")
|
||||
(print-dots-rec(- num-of-dots 1)))))
|
||||
|
||||
;; Take a list and return the number of times the symbol "a" occurs in it
|
||||
(defun count-a-symbols(lst)
|
||||
(do ((new-lst lst (cdr new-lst))
|
||||
(n 0 (+ n (if (eq (car new-lst) 'a) 1 0))))
|
||||
((not new-lst) n)))
|
||||
|
||||
;; Now do a recursive version (which is probably easier)
|
||||
(defun count-a-symbols-rec(lst)
|
||||
(if lst
|
||||
(+ (if (eq (car lst) 'a)1 0) (count-a-symbols-rec(cdr lst)))
|
||||
1))
|
||||
Reference in New Issue
Block a user