(Basic Building Blocks in LISP) Basic Syntax:-
LISP
programs are made up of three basic building blocks −
- atom
- list
- string
Atom:-
An atom is a number or string of contiguous
characters. It includes numbers and special characters. Examples of some valid
atoms −
- hello-from-digital-classes
- name
- 123008907
List:-
A list is a sequence of atoms and/or other
lists enclosed in parentheses. Examples of some valid lists
- ( i am a list)
- (a ( a b c) d e fgh)
- (sun mon tue wed thur fri sat)
String:-
A string is a group of characters
enclosed in double quotation marks. Examples of some valid strings
- " I am a string"
- "a ba c d efg #$%^&!"
- "Please enter the following details :"
Adding Comments:-
The semicolon symbol (;) is used for
indicating a comment line.Example:-
(write-line "Hello World")
; greet the world
Some Notable Points
- The basic numeric operations in LISP are +, -, *, and /
- LISP represents a function call f(x) as (f x), for example cos(45) is written as cos 45
- LISP expressions are case-insensitive, cos 45 or COS 45 are same.
Naming Conventions in LISP:-
Name or symbols can consist of
any number of alphanumeric characters other than whitespace, open and closing
parentheses, double and single quotes, backslash, comma, colon, semicolon and
vertical bar. To use these characters in a name, you need to use escape
character (\).A name can have digits but not entirely made of digits, because
then it would be read as a number.
Use of Single Quotation Mark:-
LISP evaluates everything
including the function arguments and list members. At times, we need to take atoms
or lists literally and don't want them evaluated or treated as function calls. To
do this, we need to precede the atom or the list with a single quotation mark. The
following example demonstrates this.
Example
(write-line "below single quote used ")
(write '(* 2 3))
(write-line " ")
(write-line "below single quote not used ")
(write (* 2 3))
;Here write-line is used for string.
Output:-
single quote used, it inhibits evaluation
(* 2 3)
single quote not used, so expression evaluated
6
0 Comments
if u have any doubts please let me know,