Lisp: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 41: Line 41:
   (format t "Read and Print value is ~a ~%" name))
   (format t "Read and Print value is ~a ~%" name))


(read-and-print *read-data*)
</syntaxhighlight>
=Changing values of variables=
We use setf to change the values of existing variables
<syntaxhighlight lang="lisp">
(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*)
(read-and-print *read-data*)
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:31, 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

(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*)