Download presentation
Presentation is loading. Please wait.
Published byJean Waters Modified over 9 years ago
1
Chapter 14
2
Lisp, a functional programming language, is the second oldest language (next to Fortran). Motivated by a need to do symbolic, rather than numerical, manipulations. Common LISP and Scheme are the two most popular dialects of the traditional LISP. Haskell is a modern functional language that adds strong typing to the more usual features. Other new functional languages: Erlang, Scala, Ocaml, even recent versions of Java,…
3
The name comes from Scalable Language Functional programming + OO concepts. Runs on the Java virtual machine Created by Martin Odersky Basis for many well-known systems today including Twitter, FourSquare, and LinkedIn Coursera has offered free online courses in Scala
4
LISt Processing The linked list is the major data structure Both programs and data can be represented this way ◦ Programs are able to manipulate code just like any other data ◦ Learning programs, …
5
“The Lisp language is primarily for symbolic data processing. It has been used for symbolic calculations in differential and integral calculus, electrical circuit design, mathematic logic, game playing, and other fields of artificial intelligence.”, 1965, from the LISP 1.5 Programmer’s Manual
6
A function f from a set X (the domain) to a set Y (the range) maps each element x in X to a unique element y in Y. For example, f(x) = x 2 maps the set of real numbers into the set of positive real numbers. ◦ i.e., the domain X is the set of all real numbers, the range is the set of positive reals.
7
If f is a function from X to Y and g is a function from Y to Z, then (g ◦ f ) (x) is a function from X to Z defined as (g ◦ f ) (x) = g(f (x)), for all x in X Simply put, it means to replace the x in g(x) with f (x). Example: ◦ f (x) = x 2 + x ◦ g(x) = 2x + 1 ◦ g ◦ f = 2*f(x) + 1 = 2(x 2 + x ) + 1
8
Example using mathematical functions: ◦ f (x) = x 2 + x ◦ g(x) = 2x + 1 ◦ g ◦ f = 2(x 2 + x ) + 1 Example using programming language function ◦ int f (x) {x 2 + x}; ◦ int g(x) {2x + 1}; ◦ Now, g(f(x)) is {2 * f(x) + 1};
9
Functional programming mirrors mathematical functions: ◦ domain = input, range = output ◦ A computation maps inputs to outputs Variables are mathematical symbols; not associated with memory locations. ◦ Compare to imperative programming where a variable represents a value that is stored in memory and can be accessed by name as long as it is alive and in scope. ◦ In functional languages a symbol is used in a computation of a value that then can be used in additional computations.
10
Pure functional programming is state-free: no assignment statements. ◦ In other words, no variables, no way to permanently change memory. Programs in pure functional languages consist of composite functions; output of each function becomes input to another. Today, most functional languages have some imperative statements.
11
Typical mathematical function: Square(n) = n 2 ◦ funcName(args) = func-definition: an expression Square : R R maps from reals to positive reals A function is total if it is defined for all values of its domain. Otherwise, it is partial. e.g., Square is total.
12
In imperative programming, variables are used to denote memory locations: x = x + 1 means “Update the program state by adding 1 to the value stored in the memory cell named x and storing the sum back in the memory cell.” ◦ x is used two ways: as an r-value and an l-value
13
Functional languages adopt the mathematical notion of a variable as something that represents an expression. ◦ There is no association with a memory location or modification of a variable’s value. So in functional languages a variable represents an immutable value. ◦ Since variables don’t get updated, there’s no concept of assignment and consequently no notion of program state.
14
The value of a function, in a functional language, depends only on the value of its arguments and not on any previous actions. Contrast to the view of functions in imperative languages, where function values are based on arguments, order of evaluation, and can also have side effects (change state). ◦ Since pure functional languages have no state they also have no side effects.
15
Referential transparency: a function’s result depends only upon the values of its arguments and not on any previous computation or the order of evaluation of its arguments. However, most functional languages today are not “pure”; they have some version of the assignment statement.
16
The lambda calculus, invented in 1941, is the foundation of functional programming It “…provides simple semantics for computation with functions so that properties of functional computation can be studied.” http://www.answers.com/topic/lambda-calculus#Motivation http://www.answers.com/topic/lambda-calculus#Motivation
17
Is a general model of computation ◦ Can express anything that can be expressed by a Turing machine; in other words, is Turing Complete An imperative language works by changing the data store; a functional language works by applying functions and returning values ◦ Lambda calculus models the latter approach ◦ Some practitioners claim that it is harder to make mistakes while using this style of programming.
18
In imperative and OO programming, functions have different (lower) status than variables. In functional programming, functions have same status as variables; they are first-class entities. ◦ They can be passed as arguments in a call. ◦ They can transform other functions.
19
A function that operates on other functions is called a functional form. e.g., g(f, [x1, x2, … ]) = [f(x1), f(x2), …], becomes g(Square, [2, 3, 5]) = [4, 9, 25] Meaning: function g takes as parameters another function and a list (sequence) of values to which the parameter function is applied.
20
Lists are written with white-space-separated elements enclosed in parentheses Expressions are represented as lists Lists are treated as functions with the first element being the operation and the remaining elements the arguments.
21
A derivative of Lisp Our subset: ◦ omits assignments ◦ simulates looping via recursion ◦ simulates blocks via functional composition Scheme is Turing complete, with recursion used to implement repetition.
22
Cambridge prefix notation for all Scheme expressions:(f x1 x2 … xn) e.g., (+ 2 2); evaluates to 4 (+(* 5 4 2)(-6 2));means 5*4*2+(6-2) Note: Scheme comments begin with ; Cambridge prefix allows operators to have an arbitrary number of arguments.
23
Used to define global “variables”; e.g. ◦ (define f 120) ◦ define is not equivalent to an assignment statement. It just gives a name to a value as a convenient notation. define can also be used for other purposes, as we will see. setQ and setF are functions that operate more like assignments.
24
Three steps: ◦ replace names of symbols by their current bindings. ◦ Evaluate lists as function calls in Cambridge prefix, where a list is a set of elements enclosed in ( ); e.g., (* f 2 ) The first element in the list is always treated as the function unless you specifically say not to. ◦ Constants evaluate to themselves.
25
5; evaluates to 5 Given the global definition (define f 120) then f; evaluates to 120 (+ f 5); evaluates to 125 (+ 5 2 9 13); evaluates to 29 #f; predefined, false 5, #f, #t are constants, f is a bound symbol
26
(+ 5 2 9 13); evaluates to 29 but (+ (5 2 9 13)); an error Scheme will try to evaluate the second list, interpreting “ 5 ” as a function (f); error - f isn’t a function
27
(define colors (quote (red yellow green))) or (define colors (‘ (red yellow green))) Quoting tells Scheme/LISP that the following list is not to be evaluated. This is a way of defining data.
28
(define x f) ; defines x as 120 (value of f) (define x ‘f) ; defines x as the symbol f (define color ‘red) ; color is defined to be red (define color red) ; error: no definition of red
29
A list is a series of expressions enclosed in parentheses. ◦ Lists represent both functions and data. ◦ The empty list is written (). ◦ e.g., (0 2 4 6 8) is a list of even numbers. ◦ Here’s how it’s stored:
30
Each list node is a record with two fields: the car and the cdr: car is (a pointer to) the first field, cdr is (a pointer to) the second. ◦ Typically referred to as cons cells Note that in the previous example the cdr of the last node ( |8|nil| ) is nil. ◦ This is equivalent to the null pointer in C++ The nil value can be represented as ( ), which is also the representation for an empty list.
31
Proper lists are assumed to end with the value ( ) which is implemented by null reference In a dotted list the last cons has some value other than nil as the value of the cdr field. ◦ “Dotted” lists are written (0 2 4 6. 8) The last node in the previous list would have been |6|8|
32
Structure of a List in Scheme Figure 14.1 (a) (b) Notice the difference between the list with nil as the last entry, and the “dotted” list shown in part b.
33
a nil c b List Elements Can Be Lists Themselves This represents the list (a (b c))
34
Because each node in a Scheme list consists of two pointers, internally the lists are similar to binary trees. Recall: The links (pointers) are called the 'car' and the 'cdr'. ◦ A list node (cell) is also called a cons or a pair.
35
car: returns the first element (“head”) of a list cdr: returns the tail of the list, which is itself a list
36
Suppose we write (define evens ‘(0 2 4 6 8)). Then: (car evens) ; gives 0 (cdr evens) ; gives (2 4 6 8) ( (null? ‘()); gives #t, or true (cdr(cdr evens)); (4 6 8) (car‘(6 8)) ; 6 (quoted to stop eval)
37
cons: used to build lists ◦ Requires two arguments: an element and a list; e.g., ◦ (cons 8 ( )) ; gives the 1-element list (8) ◦ (cons 6 (cons 8( ))) ; gives the list (6 8) ◦ (cons 6 ‘(8)) ; also gives the list (6 8) ◦ (cons 4(cons 8 9)) ; gives the dotted list ; (4 8. 9 ) since 9 is not a ; list
38
list takes an arbitrary number of arguments and creates a list; append takes two lists and concatenates them: ◦ (append ‘(1 3 5) evens) ;gives(1 3 5 0 2 4 6 8) ◦ (append evens (list 3) ; gives (0 2 4 6 8 3) ◦ (list ‘(1 3 5) evens) ; gives ((1 3 5) (0 2 4 6 8)) ◦ (list 1 2 3 4); gives (1 2 3 4) ◦ (list ‘evens ‘odds);gives (evens odds)
39
The function equal? returns true if the two objects have the same structure and content. (equal? 5 ‘(5)) ; gives #f, or false (equal? 5 5); #t (equal? ‘(1 2 3) ‘(1 (2 3))) ; returns false
40
Numbers: integers, rationals, and floating point numbers Characters Functions Symbols Strings Boolean values #t and #f All values except #f and ( ) are interpreted as true when used as a predicate.
41
(if test then-part) (if test then-part else-part) Example: (if (< x 0) (- 0 x)) ;returns –x if x<0 (if (< x y) x y) ; returns the smaller of x, y
42
The case statement is similar to a Java or C++ switch statement: (case month (( sep apr jun nov) 30) ((feb) 28) (else 31) ; optional ) All cases take an unquoted list of constants, except for the else.
43
define is also used to define functions; according to the following syntax: (define name (lambda (arguments) body)) or (define (name arguments) body) From the former, based on Scheme as an example of an applied lambda calculus.
44
The lambda calculus is based on the notion of variable bindings in functions. ◦ The lambda is arbitrary – doesn’t have any mnemonic significance Lambda calculus was invented in 1945 as a way of studying logical formalisms & has relevance in computer science, especially programming language theory.
45
(define (min x y) (if (< x y) x y)) name argsbody to return the minimum of x and y (define (abs x)(if(< x 0)(- 0 x) x)) to find the absolute value of x
46
;; bulls-eye? : Number Number -> Boolean ;; par1 par2 result ;; determines if(x,y)is in the bull's eye (define (bulls-eye? x y) (<= (dist-to- origin x y) 5)) name args body ;; dist-to-origin: Number Number -> Boolean ;; determines distance from (0,0) to (x,y) (define (dist-to-origin x y) (sqrt (+(* x x) (* y y)))) name argsbody
47
(define (factorial n) (if(< n 1) 1 (* n factorial(-n 1))) )) (define (fun1 alist) (if (null? alist) 0 (+ (car alist)(fun1(cdr alist))) )) ◦ Suppose ‘alist’ is replaced by ‘evens’
48
length: returns the number of elements in a list ◦ (length ‘(1 2 3 4)); returns 4 ◦ (length ‘((1 2 3) (5 6 7) 8)) ; returns 3 (define (length alist) (if (null? alist) 0 (+ 1 (length (cdr alist))) ))
49
member: tests whether an element is in a list (member 4 evens);returns (4 6 8) (member 1 ‘(4 2)); returns () If the element is in the list, return portion of the list starting with that element, else return the empty list.
50
The mapcar function takes two parameters: a function fun and a list alist. mapcar applies the function to the list of values, one at a time, and returns a list of the results.
51
(define (mapcar fun alist) (if (null? alist) ‘( ) (cons (fun (car alist)) (mapcar fun (cdr alist))) )) E.g., if (define (square x) (* x x)) then (mapcar square ‘(2 3 5 7 9)) returns (4 9 25 49 81)
52
14.2.9 Symbolic Differentiation Symbolic Differentiation Rules Fig 14.2
53
(define (diff x expr) (if (not (list? expr)) (if (equal? x expr) 1 0) (let ((u (cadr expr)) (v (caddr expr))) (case (car expr) ((+) (list ‘+ (diff x u) (diff x v))) ((-) (list ‘- (diff x u) (diff x v))) ((*) (list ‘+ (list ‘* u (diff x v)) (list ‘* v (diff x u)))) ((/) (list ‘div (list ‘- (list ‘* v (diff x u)) (list ‘* u (diff x v))) (list ‘* u v))) ))))
54
Usually implemented as an interpreted language, although there are compiled forms. A LISP interpreter generally consists of: ◦ a memory manager ◦ collection of core functions implemented in compiled form for speed ◦ a set of support functions implemented with the core
55
Unlike most other languages, no distinction is made between "expressions" and "statements" Code and data are written as expressions. When an expression is evaluated, it produces a value (or list of values), which then can be embedded into other expressions.
56
In Scheme, processing proceeds by typing expressions into the Scheme environment. After each expression is entered, Scheme ◦ reads the expression, ◦ evaluates the expression to determine its value, and ◦ prints the resulting value. ◦ This read-eval-print cycle forms the basis for all processing within Scheme.
57
Memory is organized into cells (or nodes). all cells are the same size, usually 2 words of memory cells form a large pool of available storage, chained together into the initial free list. cells are taken off the free list as needed to build atoms, lists, etc.
58
Literal atoms may require several cells to hold their definition (Remember the list diagram) Each cell must be big enough to hold a pointer to the next list element, and a pointer to the current element value
59
Evaluations in LISP take needed cells off of the free list. Many results are temporary and then are of no more use. Therefore, must have mechanism for reclaiming space. Lisp was the first language to use automatic garbage collection extensively
60
Another Lisp Quote –Lisp has jokingly been called "the most intelligent way to misuse a computer". I think that description is a great compliment because it transmits the full flavor of liberation: it has assisted a number of our most gifted fellow humans in thinking previously impossible thoughts. –— Edsger Dijkstra, CACM, 15:10Edsger Dijkstra
61
Event-Driven Concurrent
62
Event-drive programs do not control the sequence in which input events occur; instead, they are written to react to any reasonable sequence of events. Compare to imperative, OO, functional, and logical paradigms in which the ordering of input influences the way program steps are executed
63
Graphical User Interface (GUI) programs Online registration systems Airline reservation systems Embedded applications for devices such as cell phones, cars, airplane navigation systems, robots, home security systems …
64
Imperative programs get input, operate on it and produce results, either in a loop or in a single pass. Programmer determines order of input. Event-driven programs maintain an event queue that corresponds to events that occur asynchronously and are processed as they happen. ◦ Event-driven programs tend to run continuously, waiting for input
65
Provides certain classes and methods that can be used to design an interaction ◦ Specify events, associate an event with an object, and provide event-handling actions Figure 16.3 shows a partial class hierarchy with subclasses such as button, popup menu, text area, etc. Listeners recognize an event, such as a mouse click.
66
Concurrency occurs when two or more separate threads or programs execute at the “same” time – actually, or apparently. Example: several objects of the same class (e.g basketBallPlayer) may model a real- world situation in which several actual players interact concurrently Example: a web browser downloads a page from a web site; several threads cooperate by rendering different parts of the page
67
Concurrency is implemented in one of two ways: Concurrent program units execute ◦ on separate processors at the same time, or ◦ on a single processor by implementing time slicing: switching back and forth rapidly between several separate programs (processes, threads)
68
Multiprogramming is an early operating system technique that time-shares CPU cycles between several different processes, all loaded in memory. ◦ Originally introduced to give the CPU something to do when a process was waiting for input ◦ Later extended by interrupting a running process to give another process a chance
69
Concurrency was first implemented at the operating system level. Later techniques extended it to application level programs. Concurrency at the application level requires some kind of synchronization to prevent possible data corruption ◦ Semaphores ◦ Monitors – Java threads ◦ Dedicated concurrent languages: concurrent Pascal, concurrent Haskell, Cilk (concurrent C), ….
70
A concurrent program is a program designed to have two or more execution contexts. Such a program is said to be multithreaded. ◦ A parallel program is a concurrent program in which several threads are active simultaneously (implies multiple processors, but conceptually the same thing) A distributed program is a concurrent program that is designed to executed simultaneously on a collection of processors that don’t share memory. ◦ Not the same – cannot share data directly, aren’t generally considered to be ‘multi-threaded’
71
Race conditions / synchronization Deadlock Interprocess communication
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.