20
ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Embed Size (px)

Citation preview

Page 1: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

ITEC 380

Organization of programming languages

Lecture 3 – Functional Programming

Page 2: 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

Page 3: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Grammar

Objectives

• Intro to lisp• Getting an interpreter working• Loading programs• Basic lists / operations• Combining / Parsing lists

Page 4: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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!!!

Page 5: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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/

Page 6: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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

Page 7: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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

Page 8: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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

))

Page 9: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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))

))

Page 10: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Grammar

Other ways

• setq– (setq Name ‘Me)

• list– (list Name ‘You and a dog named boo)

Page 11: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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)

Page 12: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Grammar

I/O

• Input data to a variable

• Output a variable

(let ((A "Hello")) (princ A))

(let ((A (read))) (princ A))

Page 13: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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

Page 14: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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))

Page 15: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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))

Page 16: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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"))

Page 17: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Grammar

Conditionals

• Can have multiple conditionals• Example

(cond ((or (> 3 5) (>2 6) ) (princ “One”))((< 2 4) (princ “Two”))

)

Page 18: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

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

Page 19: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Grammar

Missing?

• Given what you know of existing programming languages, what is currently missing?

• How do we work around these missing features?

Page 20: ITEC 380 Organization of programming languages Lecture 3 – Functional Programming

Grammar

Next week

• Functional programming - Lisp