Transcript Slide 1

ITEC 380
Organization of programming
languages
Lecture 3 – Functional
Programming
Review
•
•
•
•
•
Grammars
BNF/EBNF
How to write a parser, why to do so
Parse trees
Types of parsers
Grammar
Objectives
•
•
•
•
•
Intro to lisp
Getting an interpreter working
Loading programs
Basic lists / operations
Combining / Parsing lists
Grammar
Lisp
• Different evolutionary path than imperative
/ OO type programming
– Much more math focused
• Removes the focus on the state of a
program from the equation
• Works on lists of numbers / strings
• Case insensitive language!!!
Grammar
Getting it
running
• Visit http://www.clisp.org/
• Once you run it, you get something like
i i i i i i i
I I I I I I I
I \ `+' / I
\ `-+-' /
`-__|__-'
|
------+------
ooooo
8
8
8
8
8
8
o
ooooo
o
ooooooo
8
8
8
8
8
8
8
8
8
8
8oooooo ooo8ooo
ooooo
ooooo
8
o 8
8
8
8
8
ooooo
8oooo
8 8
o
8 8
ooooo
8
Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>
Copyright
Copyright
Copyright
Copyright
Copyright
(c)
(c)
(c)
(c)
(c)
Grammar
Bruno Haible, Michael Stoll 1992, 1993
Bruno Haible, Marcus Daniels 1994-1997
Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Bruno Haible, Sam Steingold 1999-2000
Sam Steingold, Bruno Haible 2001-2010
Ways to use
• Can pipe a program into it
– clisp < filename.txt
• Can type code straight into the shell
– (+ 3 2)
• Can load code from files
– (load “filename.txt”)
– Note: define functions in file, run by hand
Grammar
Step 1
• Start with the most evil part of any
programming language: Global variables
• Example
(defparameter *tiny* 1)
(defparameter *large* 10)
*tiny*
*large*
Note:
* Is just a stylistic convention
Grammar
Step 2
• Create a function accomplish a task
• Example
(defparameter *small* 1)
(defparameter *big* 10)
(defun addtwo ()
(*
(- *big* *small*)
2
)
)
• What does this print out?
• Does it matter where the )’s are
Grammar
Local
parameters
• Let = freedom
(defun localTest()
(let ((a 5) (b 6))
(c(+ a b))
)
)
• What are the advantages of local versus
global?
• Can create local functions with flet
• Can allow recursion with local functions with
labels
Grammar
Other
ways
• setq
– (setq Name ‘Me)
• list
– (list Name ‘You and a dog named boo)
Grammar
Data
• Numbers
– 4/6 => How would this be put into lisp?
– 4.0/6 => Ditto?
• Strings
– Enclose in “ “ otherwise will be treated like a
function
• Items as a list
– ‘(+ 2 1) versus (+ 2 1)
Grammar
I/O
• Input data to a variable
(let ((A (read))) (princ A))
• Output a variable
(let ((A "Hello")) (princ A))
Grammar
Working
with lists
• Three basic functions
• cons
– Combine lists together
• car
– Access the first item in a list
• cdr
– Return the second element to the end of a list
Grammar
Flexibility
• Can combine multiple car and cdrs
together
cadr
cddr
cadar
Apple Peach Mango Pear Strawberry Blueberry Pineapple
(Apple (Red Yellow Pink Lady)
Peach (Georgia California)
)
• Can also cheat by using
– First, second, third, fourth, etc…
Grammar
Inside lists
• How to check if a number is in a list
– (member 5 ‘(3 4 5))
• How access a particular item in the list
– (find-if #’oddp ‘(1 2 4))
Grammar
Conditionals
• Simple to use
• (if (conditional) (then) (else) )
(if (> 5 3)
(princ "Larger")
(princ "Smaller")
)
• Sometimes you will make a function call
instead of the ‘(list data)
• Also have when and unless
Grammar
Conditionals
• Can have multiple conditionals
• Example
(cond
((or (> 3 5) (>2 6) ) (princ “One”))
((< 2 4) (princ “Two”))
)
Grammar
Functions
• What is out there?
– abs,sin, cos, tan, mod, round, min, max, expt,
sqrt
– first,rest,last, length
– cons, append, list
• Type checking
– listp,numberp,integerp,stringp, evenp, oddp
• Others
– null, equal/eql/eq, and, or, not
– Use eq for symbols, equal for all others
Grammar
Missing?
• Given what you know of existing
programming languages, what is currently
missing?
• How do we work around these missing
features?
Grammar
Next week
• Functional programming - Lisp
Grammar