Lecture 3 - FPL - LISP and Haskell
Lecture 3 - FPL - LISP and Haskell
Lisp is the second-oldest high-level programming language after Fortran and has changed
a great deal since its early days, and a number of dialects have existed over its history.
Today, the most widely known general-purpose Lisp dialects are Common Lisp and
Scheme.
Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute
of Technology (MIT).
It was first implement by Steve Russell on an IBM 704 computer.
Applications built in Lisp
Let us write an s-expression to find the sum of three numbers 7, 9 and 11. To do this, we
can type at the interpreter prompt.
If you would like to run the same program as a compiled code, then create a LISP source
code file named myprog.lisp and type the following code in it.
https://rextester.com/l/common_lisp_online_compiler
Lisp uses Prefix Notation
In the previous program the + symbol works as the function name for the process of
summation of the numbers.
In prefix notation, operators are written before their operands. For example, the expression,
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.
Data Types
Global Variables
have permanent values throughout the LISP system and remain in effect until a new value is
specified
generally declared using the defvar construct.
Since there is no type declaration for variables in LISP, you directly specify a value for a symbol
with the setq construct.
Variables
Local Variables
defined within a given procedure
accessible only within the respective function
an also be created using the setq construct
Operators
a symbol that tells the compiler to perform specific mathematical or logical manipulations
Arithmetic Operations
Comparison Operations
Logical Operations
Arithmetic Operations
A = 10, B = 20
Arithmetic Operations
A = 10, B = 20
Comparison Operations
A = 10, B = 20
Comparison Operations
A = 10, B = 20
Logical Operations on Boolean Values
A = nil, B = 5
Logical Operations on Boolean Values
A = nil, B = 5
Decision Making
used for checking multiple test-action clauses. It can be compared to the nested if
statements in other programming languages
If - else
Functions
functions that test their arguments for some specific conditions and returns nil if the
condition is false, or some non-nil value is the condition is true
Predicates
Predicates
Predicates
Predicates
Lists
the most important and the primary composite data structure in traditional LISP
single linked lists
constructed as a chain of a simple record structure named cons linked together
The cons Record Structure
Member function searches a list for the first occurrence of an element (item) satisfying
the test. Return value is tail of the list starting from found element or NIL when item is not
found.
Example – user-defined member function
Example – Lisp append function
Append function returns a new list that is the concatenation of the copies. lists are left
unchanged;
Example – user-defined append function
Example - double
Write Common Lisp function to find the double of the given value.
Example – all positives
Write Common Lisp function to find if the given numbers are all positive.
Example – sign
Write Common Lisp function to find if the sign of a given number (+, - or 0)
Haskell
Values are terms, such as 5 (an integer number), "Hello World!" (a character string), and
3.141 (a floating point number).
Values are processed by functions.
addition + takes two numbers and produces a new number, namely the sum of the two input
values;
++ takes two strings and produces a new string by concatenating the two input strings;
length takes a string and produces a number, namely the length of the input string.
In other words, functions, such as +, ++, and length, are mappings from input values to
output values.
Values
We write 1 :: Int or "Hello" :: String to indicate that the values 1 and Hello have the the type Int
and String, respectively. Hence, 1 :: Int can be read as “1 has type Int”.
Functions
The head consists of the name of the function as well as names for the arguments to the
function.
Functions
There are some syntactic restrictions for variable and function names in Haskell: the name
of a function or variable
has to start with a lower case letter or _ (underscore) and
may only contain letters, digits, _ (underscore), or ' (apostrophe)
Moreover, when defining new functions, we have to be careful not to use a function name
that does already carry a meaning, such as length.