ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.

17
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming

Transcript of ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.

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

ITEC 380

Organization of programming languages

Lecture 5 – Functional Programming

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

LISP

Review

• List manipulation 2.0• Association• A touch of graph theory• Map / reduce• Homework– Questions?

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

LISP

Objectives

• In class coding• Lambda• Loops• Lazyness• Structures/Classes

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

LISP

In-class coding

• Problems– Given a list, return the average if all of

the contents are integers, or return nil if there is a non-integer in the list

– Given a list of items as a parameter to your function, return true if there are any duplicates, nil other wise

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

LISP

Lambda

• OO version– Inner classes– Example

class AnExample{

public void function(){

class MyActionListener : ActionListener{

public void actionPerformed(){}}JButton clicker = new Jbutton();clicker.setActionListener(new MyActionListener());

}}

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

LISP

Lambda

• Lisp version

• Allows you to define a function wherever you need to use one

• What are the pros / cons of this method?

(lambda (n) (n/2))

(mapcar (lambda (n) (/ n 2)) '(8 10 12))

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

LISP

Looping

• Non-functional type command• Allows for typical for loop tasks, uses different syntax

• For creates a local variable and iterates through a list• Stopped by sentinel named below• Sum is the usual• Other tricks

– For I from 5 to 10 – For I in ‘(1 2 3)– For I below 5 do (function)– For I below 10 when (condition) sum I – Collect (operation) //Produces a list

(loop for i below 5 sum i)

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

LISP

Lazyness

• Issue–Whenever lisp gets information, it

evaluates it– Given a lisp program that can play

chess, how long will it take to handle a move if all possible moves are analyzed?

– Example (defun sub (x y)

(princ “Calculation”)(- x y)

)(defparameter *test* (lazy (sub 5 3))(force *foo*)

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

LISP

Problem

• Not part of common lisp, so how do we create it?

• Macros – Unevaluated lisp code that is returned in proper form

• Example(defmacro Square(x)

‘(* X X))

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

LISP

Why?

• Compile vs coding time• Example of the when implementation

(defmacro when (condition &rest body) `(if ,condition (progn ,@body)))

(when (> 5 3)(princ ‘5 is greater than 3)

)

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

LISP

End-result

• Code(defmacro lazy (&body body)

(let ((forced (gensym)) (value (gensym))) `(let ((,forced nil) (,value nil)) (lambda ()

(unless ,forced (setf ,value (progn ,@body)) (setf ,forced t))

,value)))) (defun force (lazy-value) (funcall lazy-value))

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

LISP

Structures

• Can group information together• Example

(defstruct triangle (base 5) (altitude 5))(setq test (make-triangle :base 7 :altitude 8))(triangle-base test)

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

LISP

Collections

• How do you wed OO / non OO together?

• What are structures in C/C++ or records in Ada?

• What are the benefits / downsides of them?

• Lisp equivalent (defclass point() ((x :type number) (y :type number)))(setq FPoint (make-instance 'point))(setf (slot-value FPoint 'x) 10)(slot-value FPoint 'x)

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

LISP

Other Features

• Constructor with default values– :initform value after the var name in defclass

• Accessibility– :reader names the function that will read the

slot– :write names the function that will write to

the slot

• Singletons– :allocation :class

• Also allows for data inheritance

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

LISP

Questions

• What do you think of the structure / OO version in Lisp?

• Why is it there?• What is the purpose / problem it is

trying to solve?• The underlying issue!

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

LISP

Functional Programming

• 2 major sections• Pure functional– Functions that access no outside

variables– Can be run in parallel

• Parts that cause side effects– Variables that hold state– Interact with the outside world

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

LISP

Next week

• Last week of functional programming – Lisp

• Logical / declarative programming coming up