ITEC 380 Organization of programming languages Lecture 3 – Functional Programming.
-
Upload
derick-mills -
Category
Documents
-
view
222 -
download
2
Transcript of ITEC 380 Organization of programming languages Lecture 3 – Functional Programming.
ITEC 380
Organization of programming languages
Lecture 3 – Functional Programming
Grammar
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• Once you run it, you get something
like i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo 8oooo `-__|__-' 8 8 8 8 8 | 8 o 8 8 o 8 8 ------+------ ooooo 8oooooo ooo8ooo ooooo 8
Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>
Copyright (c) Bruno Haible, Michael Stoll 1992, 1993Copyright (c) Bruno Haible, Marcus Daniels 1994-1997Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998Copyright (c) Bruno Haible, Sam Steingold 1999-2000Copyright (c) Sam Steingold, Bruno Haible 2001-2010
http://www.clisp.org/
Grammar
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
• What does this print out?• Does it matter where the )’s are
(defparameter *small* 1)(defparameter *big* 10)(defun addtwo () (*
(- *big* *small*) 2
))
Grammar
Local parameters
• Let = freedom
• What are the advantages of local versus global?
• Can create local functions with flet• Can allow recursion with local functions with
labels
(defun localTest() (let ((a 5) (b 6)) (c(+ a b))
))
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
• Output a variable
(let ((A "Hello")) (princ A))
(let ((A (read))) (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
• Can also cheat by using– First, second, third, fourth, etc…
cadrcddrcadar
Apple Peach Mango Pear Strawberry Blueberry Pineapple
(Apple (Red Yellow Pink Lady) Peach (Georgia California))
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) )
• Sometimes you will make a function call instead of the ‘(list data)
• Also have when and unless
(if (> 5 3) (princ "Larger")
(princ "Smaller"))
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