Lisp: Difference between revisions
Line 81: | Line 81: | ||
I had to read about this because of my stuffed up brain. In lisp there is some referred to as a quote. And they kept mentioning shorthand but they were incredible unclear what for. and it what the function quote. What they meant is '''`''' means quote () | I had to read about this because of my stuffed up brain. In lisp there is some referred to as a quote. And they kept mentioning shorthand but they were incredible unclear what for. and it what the function quote. What they meant is '''`''' means quote () | ||
<syntaxhighlight lang="lisp"> | <syntaxhighlight lang="lisp"> | ||
(format t "Quote is ~a ~%" (+ 1 2)) ; 3 | (format t "Quote is ~a ~%" (+ 1 2)) ; Quote is 3 | ||
(format t "Quote is ~a ~%" (quote (+ 1 2))) ; + 1 2 | (format t "Quote is ~a ~%" (quote (+ 1 2))) ; Quote is (+ 1 2) | ||
(format t "Quote is ~a ~%" `(+ 1 2)) ; + 1 2 | (format t "Quote is ~a ~%" `(+ 1 2)) ; Quote is (+ 1 2) | ||
</syntaxhighlight> | |||
=Equality= | |||
Equality in Lisp has three approaches | |||
* For symbols we use eq | |||
* For numbers we use = | |||
* For strings we use equal | |||
<syntaxhighlight lang="lisp"> | |||
;; Symbols | |||
(format t "Are 'hello' and 'hello' equal? ~a ~%" (eq 'hello 'hello)) | |||
;; Numbers | |||
(format t "Are 1 and 1 equal? ~a ~%" (= 1 1)) ; T | |||
(format t "Are 1 and 2 equal? ~a ~%" (= 1 2)) ; NIL | |||
;; Strings | |||
(format t "Are 'hello' and 'hello' equal? ~a ~%" (equal "hello" "hello")) | |||
</syntaxhighlight> | |||
We can compare things with the same value but presented differently with equalp | |||
<syntaxhighlight lang="lisp"> | |||
;; equalp is used to compare numbers | |||
(format t "Are 1 and 1.0 equal? ~a ~%" (equalp 1 1.0)) ;T | |||
(format t "Are 1.01 and 1.010 equal? ~a ~%" (equalp 1.01 1.010)) ;T | |||
(format t "Are 1.010 and 1.01 equal? ~a ~%" (equalp 1.010 1.01)) ;T | |||
;; for strings for case insensitive | |||
(format t "Are 'hello' and 'HELLO' equal? ~a ~%" (equalp "hello" "HELLO")) ;T | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 02:05, 7 September 2024
Introduction
This is a quick tour on lisp. I decided to look at this because of the build a compiler youtube by lens_r which made me feel I am missing something. This has been rabbit hole which I have fallen done
Concepts
This is my view and maybe wrong but to me the first thing I noticed was the huge amount of parentheses which are involved. It made the language look really ugly and made me feel don't do this. I think I have touched this in the past because of a gun to my head. That said, like people, you probably need to understand before having a view, and even then, not for you is a better view and it is negative. So here is the half day I spent
Comments
;;;; Describe Program 4 semi colons
;;; Comments
;; Indented Comments
#|| Multiline
My Comments
||#
Format to terminal
(format t "Hello World ~%")
(print "Hello World")
Variables
To declare
(defvar *my-variable* "Hello World" )
It is common practice for local variable to have asterixes
We can declare variables for functions too, in this case read-data now is the built-in read function.
(defvar *read-data* (read))
Functions
We can of course define functions. So putting the above together we are prompted for a value and it prints it to the terminal. Not the ~% is the linefeed in list and ~a is a formatter like printf
(defvar *read-data* (read))
(defun read-and-print (name)
(format t "Read and Print value is ~a ~%" name))
(read-and-print *read-data*)
Changing values of variables
We use setf to change the values of existing variables
(defvar *read-data* (read))
;; You can change a value using setf
(setf *read-data* "Hello World 1")
(read-and-print *read-data*)
(setf *read-data* "Hello World 2")
(read-and-print *read-data*)
Formatting
This is like printf
(format t "PI to 5 characters is ~5f ~%" 3.14159)
(format t "PI to 4 decimal places is ~,4f ~%" 3.14159)
Cells
A Cell, I think is a set of parentheses and we can embed them inside each other. So
(format t "Text with this ~d ~%" (+ 5 4))
; Add a cell within a cell
(format t "Text with this ~d ~%" (+ 5 (+ 4 3)))
;; and again
(format t "Text with this ~d ~%" (+ 5 (+ 4 (+ 100 5))))
Maths Stuff
Just some examples. This is a whirlwind tour not a tutorial
(format t "Modulus of 10/3 is ~d ~%" (mod 10 3))
(format t "Remainder of 10/3 is ~d ~%" (rem 10 3))
;; All work the same way e.g. sqrt
(format t "Square root of 9 is ~d ~%" (sqrt 9))
;; and floor
(format t "Floor of 9.9 is ~d ~%" (floor 9.9))
The blasted quote
I had to read about this because of my stuffed up brain. In lisp there is some referred to as a quote. And they kept mentioning shorthand but they were incredible unclear what for. and it what the function quote. What they meant is ` means quote ()
(format t "Quote is ~a ~%" (+ 1 2)) ; Quote is 3
(format t "Quote is ~a ~%" (quote (+ 1 2))) ; Quote is (+ 1 2)
(format t "Quote is ~a ~%" `(+ 1 2)) ; Quote is (+ 1 2)
Equality
Equality in Lisp has three approaches
- For symbols we use eq
- For numbers we use =
- For strings we use equal
;; Symbols
(format t "Are 'hello' and 'hello' equal? ~a ~%" (eq 'hello 'hello))
;; Numbers
(format t "Are 1 and 1 equal? ~a ~%" (= 1 1)) ; T
(format t "Are 1 and 2 equal? ~a ~%" (= 1 2)) ; NIL
;; Strings
(format t "Are 'hello' and 'hello' equal? ~a ~%" (equal "hello" "hello"))
We can compare things with the same value but presented differently with equalp
;; equalp is used to compare numbers
(format t "Are 1 and 1.0 equal? ~a ~%" (equalp 1 1.0)) ;T
(format t "Are 1.01 and 1.010 equal? ~a ~%" (equalp 1.01 1.010)) ;T
(format t "Are 1.010 and 1.01 equal? ~a ~%" (equalp 1.010 1.01)) ;T
;; for strings for case insensitive
(format t "Are 'hello' and 'HELLO' equal? ~a ~%" (equalp "hello" "HELLO")) ;T