Functional Programming Here we briefly look at what functional programming is and why we want to study it –a function, in the mathematical sense, is a.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Unit 6 Assignment 2 Chris Boardley.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
2015 Pre-Release Practice Click the button to try a random exam-style question. Click on the reveal button to check your answer.
CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
Dr. Ken Hoganson, © August 2014 Programming in R STAT8030 Programming in R COURSE NOTES 1: Hoganson Programming Languages.
Comp 245 Data Structures Software Engineering. What is Software Engineering? Most students obtain the problem and immediately start coding the solution.
PRACTICAL COMMON LISP Peter Seibel 1.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Iteration Chapters 6 & 7. Iteration in LISP n LISP (unlike Prolog) allows iteration –mapcar, remove-if(-not), count-if, find-if for special purpose iteration.
Subprograms1 THE CALL STATEMENT (chp. 16) Purpose: a calling that executes another program; allows portions of code to be treated as a “black box”. Syntax.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Macros You might recall from earlier that some of the statements we use are not actually functions but macros –In CL, a function evaluates all of its parameters.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
PRACTICAL COMMON LISP Peter Seibel 1.
11 Speed & Debug.  Lisp is really two languages:  A language for writing fast programs  A language for writing programs fast  In the early stage,
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Variables, Values, Atoms, Lists Variables in Lisp differ from other languages –They are not declared by type their type is determined at run-time, and.
PRACTICAL COMMON LISP Peter Seibel 1.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
Search as a problem solving technique. Consider an AI program that is capable of formulating a desired goal based on the analysis of the current world.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Tail Recursion.
Modern Programming Languages Lecture 20 Fakhar Lodhi
Getting Started with Lisp
LISP LISt Processing.
Functional Languages Early in AI research, there was a need for symbolic computing handling symbols instead of numbers or strings recursion Design goals.
Functional Programming
J.E. Spragg Mitthögskolan 1997
Modern Programming Languages Lecture 21 Fakhar Lodhi
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
LISP LISt Processing.
The structure of programming
Abstraction and Repetition
Lisp: Using Functions as Data
LISP LISt Processing.
Common Lisp II.
The general format of case is the following: (case <key form>
LISP primitives on sequences
Presentation transcript:

Functional Programming Here we briefly look at what functional programming is and why we want to study it –a function, in the mathematical sense, is a set of operations that perform some computation on the parameter(s) passed, and return a value –a function, in a programming language, is a set of code whose purpose is to compute based on the parameter(s) and return a value Functions are often used to promote modularity –break down program tasks into small, roughly independent pieces –this promotes structured programming in terms of design –this also aids debugging, coding and maintenance However, the function, as we see them in programming languages, does not necessarily reflect a mathematical function

Why Not? Functions may act more like procedures –a procedure is another unit of modularity –the idea behind a procedure is to accomplish one or more related activities –the activities should make up some logical goal of the program but may not necessarily be based on producing a single result –procedures may return 0 items or multiple items unlike functions In languages like C, there are no procedures, so functions must take on multiple roles –mathematical types of functions –functions that act as procedures Such functions can produce side effects –mathematical functions do not produce side effects

Functional Design Functions should –be concise accomplish only a single task or goal –return one item in CL, if we need to return multiple values, we can wrap them into a list (or other structure) –there is also a way to have CL functions return multiple values although that is generally discouraged –have no side effects because the assignment operations are done by function calls, the function calls must produce side effects in such circumstances, but in general, YOUR code should not produce side effects –destructive operations are available and are often much more efficient than their non-destructive counterparts, but should not be used haphazardly or just because they are more efficient –use parameter passing for communication rather than global variables –exploit recursion when possible this simplifies the body of a function and requires fewer or no local variables

Comparison Example (defun bad-reverse (lis) (let* ((size (length lis)) (limit (truncate (/ size 2))) temp) (dotimes (i limit) (setf temp (nth i lis)) (setf (nth i lis) (nth (- size i 1) lis)) (setf (nth (- size i 1) lis) temp)) lis)) (defun better-reverse (lis) (let ((temp nil) (size (length lis))) (dotimes (i size) (setf temp (append temp (list (nth (- size i 1) lis))))) temp)) (defun best-reverse (lis) (if (null lis) nil (cons (car (last lis)) (best-reverse (butlast lis))))) The most efficient version but also destructive Non-destructive, less efficient because of the loop and use of temp Recursive with no local variables

CL Functions/Operations to be Avoided –set –setq –setf –psetf –psetq –incf –decf –push –pop –pushnew –rplaca –rplacd –rotatef –shiftf –remf –remprop –remhash The following are all destructive with side effects We might wish to minimize their usage: Obviously, we will need to use some of these, perhaps often (such as setf) We can avoid using these if we implemented functions with recursion instead of iteration, but that will not usually be practical (most of us are not good at recursion) Note that this does not include the n-destructive functions like nconc and nreverse since there are non-destructive counterparts, these should be avoided

Advantages? of Functional Programming Easier to design –everything is a concise module Easier to debug –if all functions are concise, they contain little code leading to easier debugging and less need to insert break statements to find where things are going wrong Can lead to more efficient programs –debatable Can lead to more efficient use of the programmer’s time –but if you aren’t good at recursion, is this going to be true? Leads to aesthetically cleaner code –yet can be more awkward to maintain Functional languages are often interpreted –this allows quick implementation and testing –this allows for incremental design and implementation of code