variables in imperative languages
play

Variables in Imperative Languages A variable in an imperative - PowerPoint PPT Presentation

Variables in Imperative Languages A variable in an imperative programming language can be Functional Programming regarded as an abstraction of what? Another representative from the Declarative paradigm Assignment is ? 1 2 Variables in


  1. Variables in Imperative Languages • A variable in an imperative programming language can be Functional Programming regarded as an abstraction of what? Another representative from the Declarative paradigm • Assignment is ? 1 2 Variables in Imperative Languages Sequences of Instructions Von Neumann architecture drives the imperative paradigm: • Imperative programs are sequences of instructions because to be executed they must be stored in memory in a sequence of • Store : individually addressable locations. locations, and because of the way the standard fetch/execute cycle works. • Machine language : sequences of instructions changing the contents of store locations (referred to by their addresses). • The control of such execution, involving loops of different kinds, is a common source of bugs in imperative programs. • High-level imperative languages : sequences of instructions changing the values of variables (referred to by their names). • This influences the whole paradigm. In particular: 1. Issues around assignment. 2. Control of execution of sequences of instructions. 3 4

  2. Assignment Referential Transparency • Referential transparency means – The meaning (or the effect, or the value) of an expression is • There are problems with assignment and the management of the same wherever (and whenever) it occurs. the store. These relate to • Imperative languages do not support referential transparency. • Side-effects • Trivial example: • Aliasing X := 0; • Referential transparency Write(X); X := X + 1; Write(X); • This problem becomes even worse when parallel execution is considered. Why? 5 6 Summary The Functional Approach • Here are two examples of function definitions (syntax may change!): • Imperative languages are based on standard computer fun double (n) = n+n; architecture. fun square (n) = n*n; • The advantage of this is run-time efficiency. • A (pure) functional program is simply a collection of function definitions. • BUT … • A functional programming system allows us to store such function definitions, and then ask for specific expressions to be evaluated . 1. It facilitates errors. • Examples: 2. It makes bug-tracing difficult. # double 7; # double( square 2 ) 3. It makes programs hard to read and understand. 14 8 4. (Backus, 1978) It limits the programmer's thinking and inhibits the problem-solving process . # map square [1, 2, 3]; [1, 4, 9] 7 8

  3. A word on the ML language (and syntax) Functional Programming We will be using ML , a general-purpose functional programming language developed by Robin Milner et al . in the 1970s at the University of Edinburgh. • Note that a functional language is inherently declarative . http://en.wikipedia.org/wiki/ML(programming_language) http://sml-family.org/ • Such function definitions are simply statements of what our … functions are. There are many dialects and (industry adopted) languages spawn from ML! We will use a freely available, web-based, simple (Oca)ML interpreter, and • Main operation is the application of functions to data (or other from now on we will adopt its syntax: functions!), different from imperative languages, whose main function is manipulating the state/store, i.e . assignment . http://try.ocamlpro.com/ The general syntax of ML is slightly different, e.g. • There is an analogy with Prolog. But remember that in Prolog we dealt with predicates, not functions, and we achieve goals fun double (n) = n+n; is let square n = n*n rather than evaluate expressions. or let rec factorial n = … for recursive functions Refer to the tutorials/docs on the web site! 9 10 (also http://caml.inria.fr/pub/docs/manual-ocaml/ ) The Functional Approach The Functional Approach • A program consists of a collection of function definitions, and a `run' • Programs in functional languages are generally shorter, easier to amounts to an evaluation of an expression involving these functions understand, design, debug, and maintain, than imperative counterpart. over input data. • The intention is to focus on the computations which are to be done, not • Function are first class objects ( higher order functions ), e.g. map . on how to fit them to the structure of the machine. • Typically recursion is used in functional programming languages, • Assignment is not a part of this paradigm, nor is the idea of a sequence iteration is used in imperative languages. of instructions. • Modern functional languages are • Under this paradigm: – are strongly typed (most type errors detected statically by the compiler, no type errors at runtime) 1. Side-effects can be completely avoided. – provide type inference. 2. Aliasing is not possible. 3. Referential transparency can be supported. • (Pure) functional programs are stateless – (good news?) 11 12

  4. FP in the Real World List of functional programming languages from homepages.inf.ed.ac.uk/wadler/realworld/ : • Industrial – Erlang, an e-commerce database, Partial evaluator for airline • Many functional languages, … scheduling, Combinators for financial derivatives • Compilers, Interpreters and Partial Evaluators, Syntax and Analysis • … and imperative ones that support forms of functional usage. Tools • Theorem Provers and Reasoning Assistants • http://en.wikipedia.org/wiki/ • Network Toolkits and Applications, Web, HTML, XML, XSLT List_of_programming_languages_by_type#Functional_languages • Natural Language Processing and Speech Recognition (pure and impure) • Numerically Based Applications, Computer Algebra – MC-SYM - computes 3D shape of nucleic acid, FFTW - Fastest Fourier Transform in the West, BlurFit - model focal plane imaging • Pure: Charity Clean Curry Haskell Hope Miranda Idris • Database Systems • Impure: C# Erlang F#, Java (since version 8) Lisp Clojure Scheme • Operating Systems Mathematica ML OCaml R Scala Python • Light and sound • There are more impure languages! – Lula: a system for theather lighting, Scheme Score 14 1. Programs 2. Data Structures • A program consists of a collection of function definitions: • Lists are the main basic data structure. • Careful with types! inOcaML + is int * int -> int +. is float * float -> float • Notations (ML): let double(n) = n +. n [2;6;4;5] is a list let square(n) = n *. n [ ] is an empty list let avg(x,y) = (x +. y) /. 2.0 List.hd [2;6;4;5] is 2 (an integer) let sqAvg(x,y) = avg(square(x),square(y)) List.tl [2;6;4;5] is [6;4;5] (a list) h :: t 2 :: [6;4;5] is … • The the following can be evaluated: • Example function involving lists (more later): square( double(6.0) ) sqAvg( 5.0 , 7.0 ) | _ -> 1 + length( List.tl l) let rec length l = match l with • In each case the system would respond with the value, respectively | [ ] -> 0 144 and 37. 15 16

  5. 3. Program Control 4. Pattern-Matching • Two equivalent definitions: • We have composition of functions : – apply one function to some value(s), and then apply another let isEmpty( l ) = if l = [ ] then true else false function to the result. (Like SqAvg above). let isEmpty( l ) = match l with • There is no notion of program loop, so recursion is essential | [ ] -> true (see the Length function above). | h :: t -> false • Functional programming also has an ` if ' construct, to distinguish • When we ask to evaluate (say) isEmpty([4,1,7]) , a matching cases. process takes place, and as a result the second line of the definition is used. • Example: • (See also the length function above.) let max (x, y) = if x >= y then x else y; let isEmpty ( l ) = if l = [ ] then true else false; 17 18 More Pattern Matching Further Pattern Matching Examples let rec addUpTo n = match n with | 0 -> 0 let rec isM x l = if l = [ ] then false | n when n>0 -> n + addUpTo (n -1);; else if x = List.hd( l ) then true else isMember x (List.tl l) let rec factorial n = match n with | 0 -> 1 | _ -> n * factorial (n - 1);; is equivalent to let rec listAsFarAs x l = match l with | [ ] -> [ ] | h :: t when h = x -> [x] let rec isM m l = match l with | h :: t -> h :: listAsFarAs x t;; | [ ] -> false | n :: t when n = m -> true let rec doubleList l = match l with | [ ] -> [ ] | h :: t -> double(h) :: doubleList t;; | n :: t when n <> m -> isM m t let rec sumList l = match l with | [x] -> x | h :: t -> h +. sumList t ;; The word pattern here refers to the formal structure of an expression ( [ ] and h :: t are patterns). Pattern-matching is an essential part of functional let listAvg l = sumList l /. (float_of_int) (length l) ;; languages. (As it is also with Prolog*.) *actually: unification 19 20

Recommend


More recommend