1 / 28

Imperative Languages

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

sides
Télécharger la présentation

Imperative Languages

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

  2. Language Use Source: http://www.computerworld.com/developmenttopics/development/story/0,10801,100542,00.html Functional Languages

  3. http://www.infoworld.com/article/05/11/30/49FErrdevelop_1.htmlhttp://www.infoworld.com/article/05/11/30/49FErrdevelop_1.html Functional Languages

  4. TIOBE Programming Community Index for April 2006 (http://www.tiobe.com/index.htm?tiobe_index) Functional Languages

  5. Languages by Category Functional Languages

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

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

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

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

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

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

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

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

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

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

  16. Lets Try It • (+ 2 2) • (defun TwoPlusTwo () (+2 2)) • (defun increment (x) (+ x 1 )) • (load something.l) • Show error (:q) • (bye) Functional Languages

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

  18. Arithmetic Functions Functional Languages

  19. Type Predicates Functional Languages

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

  21. Factorial ( defun factorial ( n ) ( cond ( ( zerop n ) 1 ) ( t ( * n ( factorial ( - n 1 ) ) ) ) ) ) Functional Languages

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

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

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

  25. List Functions Functional Languages

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

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

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

More Related