• 290 likes • 425 Views
Imperative Languages. Imperative programming describe computation in terms of state and statements that change the state An object has state, behavior and identity Variables, subroutines/functions Chances are the vast majority of your programming has been in the imperative style
E N D
Imperative Languages • Imperative programming describe computation in terms of state and statements that change the state • An object has state, behavior and identity • Variables, subroutines/functions • Chances are the vast majority of your programming has been in the imperative style • BASIC, Java, C#, Perl, PHP, Ruby, … Functional Languages
Language Use Source: http://www.computerworld.com/developmenttopics/development/story/0,10801,100542,00.html Functional Languages
http://www.infoworld.com/article/05/11/30/49FErrdevelop_1.htmlhttp://www.infoworld.com/article/05/11/30/49FErrdevelop_1.html Functional Languages
TIOBE Programming Community Index for April 2006 (http://www.tiobe.com/index.htm?tiobe_index) Functional Languages
Languages by Category Functional Languages
Functional Programming • Treats computation as the evaluation of mathematical functions • Defines output of a program as a mathematical function of inputs • No notion of internal state • No side effects • Emphasizes the definition of functions rather than the implementation of state machines • A purely function program does not use mutation, nor does it use variables • New values are constructed from existing values Functional Languages
LISP • The LISt Processor was developed by John McCarthy in the late 1950s • Was based on the “lambda calculus” developed by Alonzo Church • There are many dialects of LISP (Scheme, T, Miranda, CLOS, …) • In the 1980s Common LISP was developed Functional Languages
The Interpreter • LISP is normally used as an interpreter • Compilers for LISP do exist • To run the interpreter • On the CS UNIX machines type gcl • For win9x/NT check out • http://www.franz.com/downloads/#acl • http://www.cs.utexas.edu/~novak/gclwin.html • Resources • http://www.apl.jhu.edu/~hall/lisp.html Functional Languages
LISP Appears Different • Syntax is very different • Lots of parenthesis • Interpreted not compiled • You create an environment for your application • Very easy to develop and test programs as you go • Lack of explicit type declarations • LISP is weakly-typed • Types are determined dynamically as the program runs • Functional, list-oriented style of programming • Recursion, recursion, recursion, … Functional Languages
Using the Interpreter • The interpreter runs what is known as a read-eval-print loop • It waits for you to enter a well-formed LISP expression • Evaluates the expressions • Prints the result Functional Languages
Basic Data Types • The two most important kinds of objects in LISP are • Atoms • Lists • Atoms are represented as sequences of characters of reasonable length • Buster, Grumpy, Mouse, a, 1234 • Lists are recursively constructed from atoms • (Buster Grumpy Mouse) ( ( a b ) ( c d ) e ) Functional Languages
Atoms • The term atom refers to just about any LISP object that is not viewed as having parts • The evaluator, known as “eval”, evaluates atoms by attempting to find a value for the atom • Numbers and symbols (t, nil, …), sometimes referred to as literal atoms, are pre-defined • Non-literal atoms are similar to variables Functional Languages
Lists • A LISP list is composed of zeo or more elements, enclosed in matching parenthesis, where an element is an atom or a list • () • (a b c ) • (10 20 30 40 50) • (a (b 10) (c (20 30)) “x”) • (+ 2 2) • List elements do not need to be the same type (i.e., a list is a heterogeneous collection of elements) Functional Languages
Functions • The interpreter treats any list as containing the name of a function followed by the arguments to the function • (function-namefirst-argsecond-arg ...) • Evaluation • function-name is checked to see if it bound to a function value • Each argument is evaluated • Argument values are bound to corresponding formal parameters (call by value) • The body of the function is evaluated Functional Languages
defun • Use defun to define your own functions in LISP • (defun <name> <parameter-list> <body>) • The name of a function can be any symbol • The parameter list should be a list of symbols which must be variables, not constants • Good programming style • Keep the body of a function reasonably short • Build small functions that perform specialized tasks and then using those as building blocks for more complicated tasks Functional Languages
Lets Try It • (+ 2 2) • (defun TwoPlusTwo () (+2 2)) • (defun increment (x) (+ x 1 )) • (load something.l) • Show error (:q) • (bye) Functional Languages
Quote • Given the following:>(defun f (x) … ) >(defun g (x) … ) >(f (g 10)) • What does the last list represent? • A call to function f with a two-element list? • A call to function f with an argument that is the result of a call to function g with argument 10? • The quote (‘) function indicates that we want to treat an item as a literal > (f ‘(g 10)) Functional Languages
Arithmetic Functions Functional Languages
Type Predicates Functional Languages
Conditionals • LISP provides a function, cond, that is a general form of a standard if-then-else(cond ((test-expr1) expr1 … exprj) ((test-expr2) expr1 … exprk)) • cond is evaluated as follows • Evaluate each test-expr in turn • When the first non-nil test-expr is found, the corresponding element is evaluated • The value of the cond is the value of the last expr that is evaluated • If none of the test-exprs is non-nil, then the value of the entire cond is non-nil Functional Languages
Factorial ( defun factorial ( n ) ( cond ( ( zerop n ) 1 ) ( t ( * n ( factorial ( - n 1 ) ) ) ) ) ) Functional Languages
Cons, List, Append • Cons adds an item to a list >( cons 1 nil ) >( cons 1 ( cons 2 nil ) ) • Place a quote mark (‘) in front of an item that you do not wish the interpreter to evaluate • List is like cons but takes any number of arguments • Append has the effect of taking all the members of the lists that are its arguments and creating a new list from those elements Functional Languages
Selectors • car (also known as first) returns the first member of a list • cdr (also know as rest) returns the remainder of a list >(car '(a s d f)) a >(car '((a s) d f)) (a s) >(cdr '(a s d f)) (s d f) Functional Languages
Examples ;; A function that takes two numeric atoms as input and returns ;; their product ( defun mult ( x y ) ( cond ( ( equal y 0 ) 0 ) ( t ( + x ( mult x ( - y 1 ) ) ) ) ) ) ;; A functions that takes a single argument and returns a count of ;; the number of lists that appear in the argument ( defun list-counter ( x ) ( cond ( ( null x ) 1 ) ( ( atom x ) 0 ) ( t ( + ( list-counter ( car x ) ) ( list-counter ( cdr x ) ) ) ) ) ) ;; A function that takes a list of numeric atoms and returns the sum (defun sum-of-ints ( n ) ( cond ( ( null n ) 0 ) ( t ( + ( car n ) ( sum-of-ints ( cdr n ) ) ) ) ) ) Functional Languages
List Functions Functional Languages
First-class Functions • A first-class value can be passed as a parameter, returned from a subroutine or assigned to a variable • Requires the ability to create new values at run time • A first-class function can be passed as a parameter or returned from a subroutine • Functions are second-class values in most imperative languages Functional Languages
Apply • Applies a function to a list of arguments • Note that the second parameter to apply is a function • Can be pre-defined • Lambda Expression • Symbol • Examples • (apply ‘+ ‘( 1 2 3 ) ) • (apply ‘(lambda ( x y ) ( * x y )) ‘(45 67)) Functional Languages
Mapcar • Mapcar applies a function repeatedly to each element in a list • Returns a list of the results • Examples >(mapcar ‘atom ‘(dog ( cat horse) fish)) (t nil t ) >(mapcar ‘(lambda (x) (* x x)) ‘(4 7 2 9)) (16 49 4 81) Functional Languages