Lisp: Difference between revisions
Line 52: | Line 52: | ||
(setf *read-data* "Hello World 2") | (setf *read-data* "Hello World 2") | ||
(read-and-print *read-data*) | (read-and-print *read-data*) | ||
</syntaxhighlight> | |||
=Formatting= | |||
This is like printf | |||
<syntaxhighlight lang="lisp"> | |||
(format t "PI to 5 characters is ~5f ~%" 3.14159) | |||
(format t "PI to 4 decimal places is ~,4f ~%" 3.14159) | |||
</syntaxhighlight> | |||
=Cells= | |||
A Cell, I think is a set of parentheses and we can embed them inside each other. So | |||
<syntaxhighlight lang="lisp"> | |||
(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)))) | |||
</syntaxhighlight> | |||
=Maths Stuff= | |||
Just some examples. This is a whirlwind tour not a tutorial | |||
<syntaxhighlight lang="lisp"> | |||
(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)) | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 01:36, 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))