0% found this document useful (0 votes)
97 views

Lecture 3 - FPL - LISP and Haskell

The document provides information about the functional programming language Lisp and Haskell. It discusses the history and applications of Lisp, its basic syntax including atoms, lists and strings. It also describes the environment, program structure, variables, data types, functions, control structures, and lists in Lisp. For Haskell, it outlines the fundamental building blocks of values, functions and types.

Uploaded by

Ena Enic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Lecture 3 - FPL - LISP and Haskell

The document provides information about the functional programming language Lisp and Haskell. It discusses the history and applications of Lisp, its basic syntax including atoms, lists and strings. It also describes the environment, program structure, variables, data types, functions, control structures, and lists in Lisp. For Haskell, it outlines the fundamental building blocks of values, functions and types.

Uploaded by

Ena Enic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

IBU

International Burch University


CEN 332 Programming Languages

Lecture 3 - Functional Programming Languages: LISP


and Haskell

Assoc. Prof. Dr. Zerina Mašetić


About Lisp

 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

 Large successful applications built in Lisp.


 Emacs
 G2
 Yahoo Store
 Axiom
 Piano
 Prototype Verification System (PVS)
Environment

 Following software is needed to execute .lisp program:


 (a) Text Editor and
 (b) The Lisp Executer

 Additionally, other tools could be used such as: Racket.


 Racket is a general-purpose programming language as well as the world’s first ecosystem
for developing and deploying new languages.
Program Structure

 LISP expressions are called symbolic expressions or s-expressions.


 The s-expressions are composed of three valid objects, atoms, lists and strings.
 LISP programs run either on an interpreter or as compiled code.
A Simple Program

 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,

will be written as:


Celsius to Fahrenheit Conversion
Basic Syntax

 LISP programs are made up of three basic building blocks:


 atom
 list
 string
 An atom is a number or string of contiguous characters. It includes numbers and special
characters.
Basic Syntax

 A list is a sequence of atoms and/or other lists enclosed in parentheses.

 A string is a group of characters enclosed in double quotation marks.


Basic Syntax

 The semicolon symbol (;) is used for indicating a comment line.


Notable Points about Lisp

 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.
 LISP tries to evaluate everything, including the arguments of a function. Only three types
of elements are constants and always return their own value
 Numbers
 The letter t, that stands for logical true.
 The value nil, that stands for logical false, as well as an empty list.
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.
Data Types

 LISP data types can be categorized as.


 Scalar types - for example, number types, characters, symbols etc.
 Data structures - for example, lists, vectors, bit-vectors, and strings.
 Any variable can take any LISP object as its value, unless you have declared it explicitly.
Variables

 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

 specify one or more conditions to be evaluated or tested by the program if


 statement or statements to be executed if the condition is determined to be true, and optionally,
 other statements to be executed if the condition is determined to be false.
If - else

 used for checking multiple test-action clauses. It can be compared to the nested if
statements in other programming languages
If - else
Functions

 macro named defun is used for defining functions. The defun macro needs three


arguments
 Name of the function
 Parameters of the function
 Body of the function
Example 1 – program that prints the average of
four numbers
Predicates

 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

 a record structure containing two components called the car and the cdr


 takes two arguments and returns a new cons cell containing the two values.
 The car function is used to access the first value and the cdr function is used to access the
second value
List Function

 used for creating lists in LISP


 can take any number of arguments and as it is a function, it evaluates its arguments
List Manipulating Functions
Example – Lisp member function

 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

 The fundamental building blocks of a Haskell program are


 values
 functions
 types
Values

 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 can combine multiple values and functions:


Types

 Values can be grouped into sets with similar properties.


 For example, values which represent integers, strings, booleans, or floating point numbers,
we call such sets of values types.
 Int = {…, -3, -2, -1, 0, 1, 2, 3, …}
 A subset of the mathematical integer type. 

 Float = {…, -1232.42342, …, 1.0, 3.141, …}


 Approx

 Double = {… , -1232.42342, …, 1.0, 3.141, …}ž


 Same as Float, but uses twice as many bits to store the information
Types

 Values can be grouped into sets with similar properties.


 For example, values which represent integers, strings, booleans, or floating point numbers, we
call such sets of values types.
 Char = {…, 'a', 'A', 'b', 'B', …'1', …, '@', '#', …}
 Characters representing letters, digits, newlines, tabs and other symbols

 String = {"", "a", "b", …, "Hi" ,"3423#", …}


 Strings are (possibly empty) sequences of characters.

 Bool = {False, True}


 Representing Boolean truth 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

 A function definition comprises a head and a body separated by an equals sign.

 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy