init
This commit is contained in:
commit
7821596c57
143
notes.lisp
Normal file
143
notes.lisp
Normal file
@ -0,0 +1,143 @@
|
||||
rlwrap -- helps deal with readline
|
||||
rlwrap sbcl -- makes it so I have history and navigation
|
||||
|
||||
Functions shoudl be verbs -- clear-screen
|
||||
variables should be nouns -- dog
|
||||
globals should be surround with asterisks --
|
||||
*some-global-var*
|
||||
It should be rare to have more than one expression in a function.
|
||||
|
||||
Instead of using variables, like we do in other languages, we should instead nest functions.
|
||||
|
||||
For exampe, in python we may do something like this:
|
||||
|
||||
|
||||
;;; Python
|
||||
def addTwoNumbers(x, y):
|
||||
sum = x + y
|
||||
return sum
|
||||
|
||||
sum = addTwoNumbers(1, 3)
|
||||
print(sum)
|
||||
|
||||
;;; Lisp
|
||||
|
||||
(defun addTwoNumbers(x y)
|
||||
(+ x y))
|
||||
|
||||
(print
|
||||
(addTwoNumbers(1 3)))
|
||||
|
||||
Of course, the python one could simply be "return x + y" but the point is to
|
||||
show the normal differences in thought, not to say that it's not possible
|
||||
to do it in a lispy way in python.
|
||||
|
||||
To copy an example in only lisp to show the differenes from
|
||||
cs.gmu.edu/~sean/lisp/LispTutorial.html
|
||||
|
||||
;;; Declaritive style (I.E. the python way above)
|
||||
|
||||
(defun my-equation (n)
|
||||
(let (x y z)
|
||||
(setf x (sin n))
|
||||
(setf y (cos n))
|
||||
(setf z (* x y))
|
||||
(+ n z)))
|
||||
|
||||
;;; Functional style
|
||||
|
||||
(defun my-equation (n)
|
||||
(+ n (* (sin n) (cos n))))
|
||||
|
||||
As we can see, we avoid variables and make it look pretty
|
||||
|
||||
|
||||
;; Closure example:
|
||||
;; So what happens here? All of these functions get to share the "account" var
|
||||
;; yet, "account" is not asseble outside the functions.
|
||||
;; Since the functions are there, the let statement doesn't get garbage
|
||||
;; collected?
|
||||
;; In a manner of speaking, defining functions within a let statement
|
||||
;; allows for a private global variable
|
||||
|
||||
;; not much different from a java or C++ object
|
||||
|
||||
(let ((account 0))
|
||||
(defun deposit ($$$)
|
||||
(setf account (+ account $$$)))
|
||||
(defun withdraw ($$$)
|
||||
(setf account (- account $$$)))
|
||||
(defun amount ()
|
||||
account))
|
||||
|
||||
;; grabbing an element from a sequence (i.e. not multi-dimentional array)
|
||||
|
||||
;; Generic
|
||||
(elt "hello world" 4)
|
||||
(elt '(yo 1 3 4) 2)
|
||||
(elt #(yo yo dur cob so 2 3) 3)
|
||||
|
||||
;; String
|
||||
(aref "hi there" 3)
|
||||
|
||||
;; list
|
||||
(nth 3 '(1 2 3 4 5 6 7))
|
||||
|
||||
;; simple-vector
|
||||
(svref #(d 1 3 f g e) 4)
|
||||
|
||||
;; P-Lists (property lists)
|
||||
|
||||
(setf my-list '(armor (head
|
||||
(dragon helmet)
|
||||
legs ()
|
||||
arms ())
|
||||
weapon (left
|
||||
(wooden-shield)
|
||||
right ())
|
||||
ring (left
|
||||
()
|
||||
right ())))
|
||||
|
||||
(getf my-list 'armor) >> (HEAD LEGS ARMS)
|
||||
(getf (getf my-list 'armor) 'head)
|
||||
|
||||
;; Let's make it less crazy
|
||||
(defun equipted-helmet (my-list)
|
||||
(getf (getf my-list 'armor) 'head))
|
||||
|
||||
;; doing something like (setf (equipted-helmet my-list) 'hammer) doesn't
|
||||
;; seem to work. Perhaps a closure would be better
|
||||
|
||||
(let ((equipment '(armor (head
|
||||
(dragon helmet)
|
||||
legs ()
|
||||
arms ())
|
||||
weapon (left
|
||||
(wooden-shield)
|
||||
right ())
|
||||
ring (left
|
||||
()
|
||||
right ()))))
|
||||
(defun get-armor()
|
||||
(getf equipment 'armor))
|
||||
(defun get-helmet()
|
||||
(getf (get-armor) 'head))
|
||||
(defun change-helmet ()
|
||||
(push (getf (getf equipment 'armor) 'head) x))
|
||||
;;(setf (getf (getf equipment 'armor) 'head) x))
|
||||
#| Etc... |# )
|
||||
|
||||
Why doesn't this work?
|
||||
|
||||
(defvar mylist '(j 1 v 2))
|
||||
|
||||
;; Works
|
||||
(setf (getf mylist 'j) 3)
|
||||
|
||||
(defun get-j ()
|
||||
(getf mylist `j))
|
||||
|
||||
;;doesn't work
|
||||
(setf (get-j) 'j)
|
||||
|
1757
quicklisp.lisp
Normal file
1757
quicklisp.lisp
Normal file
File diff suppressed because it is too large
Load Diff
26
test.lisp
Normal file
26
test.lisp
Normal file
@ -0,0 +1,26 @@
|
||||
(let ((equipment '(armor (head
|
||||
(dragon-helmet)
|
||||
legs (empty)
|
||||
arms (empty))
|
||||
weapon (left
|
||||
(wooden-shield)
|
||||
right (empty))
|
||||
ring (left
|
||||
(empty)
|
||||
right (empty)))))
|
||||
(defun get-armor()
|
||||
(getf equipment 'armor))
|
||||
(defun get-helmet()
|
||||
(getf (get-armor) 'head))
|
||||
(defun get-legs()
|
||||
(getf (get-armor) 'legs))
|
||||
(defun get-arms()
|
||||
(getf(get-armor) 'legs))
|
||||
|
||||
(defun change-helmet (helmet)
|
||||
"Useage: (change-helmet 'god-helmet"
|
||||
(setf (car (get-helmet)) helmet))
|
||||
(defun change-legs (legs)
|
||||
(setf (car (get-legs)) legs))
|
||||
(defun change-arms (arms)
|
||||
(setf (car (get-arms)) arms)))
|
5
tktest/README
Normal file
5
tktest/README
Normal file
@ -0,0 +1,5 @@
|
||||
Make sure TK is installed. In mint I was able to:
|
||||
|
||||
tux i wish
|
||||
|
||||
Other distros may require TK since apt switched to package TK from WISH
|
7
tktest/build-script.sh
Executable file
7
tktest/build-script.sh
Executable file
@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
sbcl --eval "(progn
|
||||
(compile-file \"ltk\")
|
||||
(load \"ltk\")
|
||||
(compile-file \"hello-world\")
|
||||
(load \"hello-world\")
|
||||
(save-lisp-and-die \"hello-world.core\"))"
|
16
tktest/example.lisp
Normal file
16
tktest/example.lisp
Normal file
@ -0,0 +1,16 @@
|
||||
(defpackage :hello-world
|
||||
(:use :common-lisp 'ltk)
|
||||
(:export #:main))
|
||||
|
||||
(in-package :hello-world)
|
||||
|
||||
(defun main ()
|
||||
(setf *debug-tk* nil)
|
||||
(with-ltk ()
|
||||
(let ((b (make-instance
|
||||
`button
|
||||
:text "Hello World!"
|
||||
:command (lambda ()
|
||||
(do-msg "Bye!")
|
||||
(setf *exit-mainloop* t)))))
|
||||
(pack b))))
|
BIN
tktest/hello-world.core
Normal file
BIN
tktest/hello-world.core
Normal file
Binary file not shown.
BIN
tktest/hello-world.fasl
Normal file
BIN
tktest/hello-world.fasl
Normal file
Binary file not shown.
16
tktest/hello-world.lisp
Normal file
16
tktest/hello-world.lisp
Normal file
@ -0,0 +1,16 @@
|
||||
(defpackage :hello-world
|
||||
(:use :common-lisp ltk)
|
||||
(:export #:main))
|
||||
|
||||
(in-package :hello-world)
|
||||
|
||||
(defun main ()
|
||||
(setf *debug-tk* nil)
|
||||
(with-ltk ()
|
||||
(let ((b (make-instance
|
||||
`button
|
||||
:text "Hello World!"
|
||||
:command (lambda ()
|
||||
(do-msg "Bye!")
|
||||
(setf *exit-mainloop* t)))))
|
||||
(pack b))))
|
BIN
tktest/ltk.fasl
Normal file
BIN
tktest/ltk.fasl
Normal file
Binary file not shown.
5342
tktest/ltk.lisp
Normal file
5342
tktest/ltk.lisp
Normal file
File diff suppressed because it is too large
Load Diff
4
tktest/run-script.sh
Executable file
4
tktest/run-script.sh
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
sbcl --core hello-world.core --noinform\
|
||||
--eval "(progn (hello-world:main) (quit))"
|
41
tktest/tk-test.lisp
Normal file
41
tktest/tk-test.lisp
Normal file
@ -0,0 +1,41 @@
|
||||
(load "~/quicklisp/setup.lisp")
|
||||
(ql:quickload :ltk)
|
||||
(use-package :ltk)
|
||||
|
||||
(defun clear-screen()
|
||||
"Clears the screen in Linux"
|
||||
(sb-ext:run-program "clear"
|
||||
'()
|
||||
:output *standard-output*
|
||||
:search t
|
||||
:wait t))
|
||||
(defun echo-durr()
|
||||
"Print to term"
|
||||
(sb-ext:run-program "echo"
|
||||
'("durr")
|
||||
:output *standard-output*
|
||||
:search t
|
||||
:wait t))
|
||||
(with-ltk ()
|
||||
|
||||
(let ((button (make-instance 'button
|
||||
:text "hello"
|
||||
:command (lambda ()
|
||||
(clear-screen))))
|
||||
(button2 (make-instance 'button
|
||||
:text "durr"
|
||||
:command (lambda ()
|
||||
(echo-durr))))
|
||||
;; Format doesn't seem to work until the gui ends
|
||||
(button3 (make-instance 'button
|
||||
:text "try format"
|
||||
:command (lambda ()
|
||||
(format *standard-output*
|
||||
"hi"))))
|
||||
(scroller (make-instance 'scrollbar
|
||||
)))
|
||||
(grid button 0 0)
|
||||
(grid button2 0 1)
|
||||
(grid button3 0 5)
|
||||
))
|
||||
|
Loading…
x
Reference in New Issue
Block a user