1 / 17

ITEC 380

ITEC 380. Organization of programming languages Lecture 5 – Functional Programming. Review. List manipulation 2.0 Association A touch of graph theory Map / reduce Homework Questions?. Objectives. In class coding Lambda Loops Lazyness Structures/Classes. In-class coding. Problems

mabli
Download Presentation

ITEC 380

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Objectives • In class coding • Lambda • Loops • Lazyness • Structures/Classes

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

  5. 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()); } }

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

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

  8. 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*)

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

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

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

  12. Structures • Can group information together • Example (defstruct triangle (base 5) (altitude 5)) (setq test (make-triangle :base 7 :altitude 8)) (triangle-base test)

  13. 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))) (setqFPoint (make-instance 'point)) (setf (slot-value FPoint 'x) 10) (slot-value FPoint 'x)

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

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

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

  17. Next week • Last week of functional programming – Lisp • Logical / declarative programming coming up

More Related