1 / 106

CSE-321 Programming Languages Introduction to Functional Programming

CSE-321 Programming Languages Introduction to Functional Programming. 박성우. POSTECH. Programming Paradigms. Structured programming C, Pascal, … Object-oriented programming C++, Java, … Logic programming Prolog, … Functional programming SML, Haskell, Objective Caml, Lisp, Scheme, F#, ….

muir
Download Presentation

CSE-321 Programming Languages Introduction to Functional Programming

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. CSE-321 Programming LanguagesIntroduction to Functional Programming 박성우 POSTECH

  2. Programming Paradigms • Structuredprogramming • C, Pascal, … • Object-oriented programming • C++, Java, … • Logic programming • Prolog, … • Functional programming • SML, Haskell, Objective Caml, Lisp, Scheme, F#, …

  3. Outline • Expressions and values • Variables • Functions • Types • Recursion • Datatypes • Pattern matching • Higher-order functions • Exceptions • Modules

  4. Imperative Language C • A program consists of commands. • command = “do something” • Nothing wrong: if (x == 1) then x = x + 1; else x = x - 1; • Nothing wrong either: if (x == 1) then x = x + 1;

  5. Functional Language OCaml • A program consists of expressions. • expression = “obtain a value” • Nothing wrong: if (x = 1) then x + 1 else x - 1 • But this does not make sense: if (x = 1) then x + 1 • what is the value if x <> 1?

  6. Evaluation • An expression “evaluates” to a value. • We “evaluate” an expression to obtain a value. Expression Value

  7. Integer Evaluation 1 + 1 2 1 - 1 0 1 * 1 1 …

  8. Boolean Evaluation 1 = 1 true 1 <> 1 false 1 <> 0 true …

  9. An Integer Expression if1 = -1then 10 else -10 iffalsethen 10 else-10 -10

  10. Values as Expressions 1 ???

  11. Outline • Expressions and values V • Variables • Functions • Types • Recursion • Datatypes • Pattern matching • Higher-order functions • Exceptions • Modules

  12. Variable Declaration - letx = 1 + 1;; val x : int = 2 • A variable x is “bound” to value 2. • From now on, any occurrence of x is replaced by 2. - let y = x + x;; val x : int = 4

  13. Local Declaration let x = 1 in let y = x + x in let x = y + y in z + z + x 8

  14. Nested Local Declaration let x = 1 in x + x let y = <expression> in y + y let y = let x = 1 in x + xin y + y

  15. Why “Local”? • let y = let • x = 1 • in • x + x • in • x + y okay???

  16. Variables are NOT variable. • The contents of a variable never change. • immutability of variables • Surprise? • That’s because you are thinking about variables in imperative programming. variables in OCaml <> variables in C

  17. Then Why Variables? • Any advantage in using variables at all? let x = 1 in let y = x + x in let z = y + y in z + z ((1 + 1) + (1 + 1)) + ((1 + 1) + (1 + 1)) VS. What if it takes 10 hours to evaluate 1?

  18. Outline • Expressions and values V • Variables V • Functions • Types • Recursion • Datatypes • Pattern matching • Higher-order functions • Exceptions • Modules

  19. When is the first time you learned the concept of function?

  20. 함수의 사전 정의 • 함수(函數): 두 변수 x, y간에 어떤 관계가 있어 x의 변화에 따라 y가 일정한 법칙으로 변화할 때 y를 x의 함수라 함. (function) (동아 마스타 국어사전)

  21. 한자 함 • 函 1. 함(함). 2. 편지(함) 3. 갑옷(함) 4. 넣을, 들일(함) 예: (書函) 서함: 책을 넣는 상자

  22. Function = 函數 = Box Number!

  23. Using a Box Number

  24. Using a Box Number - Generalized … …

  25. Function in OCaml= Box Number fun x -> x + 1 n =

  26. Function Application • We “apply” (fun x -> x + 1) to n. • x is called a formal argument/parameter. • n is called an actual argument/parameter. … (fun x -> x + 1) n

  27. … Evaluating a Function Application (fun x -> x + 1) n n+ 1

  28. Functions in OCaml • Nameless function • fun x -> x + 1;; • Storing a nameless function to a variable • let incr = fun x -> x + 1;; • Function declaration • let incr x = x + 1;;

  29. Function Applications incr 1 (fun x -> x + 1) 1 1 + 1 2

  30. First-Class Stamps

  31. First-class Objects • First-class objects = primitive objects • can be stored in a variable. • can be passed as an argument to a function. • can be returned as a return value of a function. • Examples: • integers • booleans • characters • floating-point numbers • …

  32. First-class Objects in C • First-class objects • integers • characters • floating-point numbers • pointers • structures • … • Functions? • Function pointers are first-class objects. • But functions are not. • Why? You cannot create new functions on the fly!

  33. Functions = First-class Objects in OCaml • Functions: • can be passed as an argument to a function. • can be returned as a return value of a function.

  34. … … … Box Number as Output such that

  35. … Box Number as Output x +x

  36. Box Number as Output x y y+x

  37. Box Number as Output x y fun y -> y+x y+x

  38. Box Number as Output x y fun y -> y+x y+x fun y -> y+x

  39. Box Number as Output x y funx -> (fun y -> y+x) fun y -> y+x y+x fun y -> y+x

  40. In OCaml • Recall the following declarations are equivalent: • let incr = funx -> x + 1;; • let incr x = x + 1;; • Then: • let add = funx -> (funy -> y + x); • let add x = funy -> y + x; • let add xy = y + x; • add can take a single argument to return a function.

  41. Adding Two Integers add 1 2 (funx -> (funy -> y + x)) 1 2 (funy -> y + 1) 2 2 + 1 3

  42. Box Number as Input ( true, false)

  43. Box Number as Input f funf -> (f true, f false) ( true, false) f f

  44. Outline • Expressions and values V • Variables V • Functions V • Types • Recursion • Datatypes • Pattern matching • Higher-order functions • Exceptions • Modules

  45. Types • A type specifies what kind of value a given expression evaluates to. • 1 + 1 : int • true && false : bool • ’A’ : char • ”hello” : string • (1, true) : int * bool • (1, -1, true) : int * int * bool • 1.0 : float • () : unit

  46. Type Preservation • An evaluation preserves the type of a given expression. Expression : T Value : T

  47. Example let x = 1 in let y = x + x in let z = y + y in z + z : int 8 : int

  48. Function Types • T -> T’ • type of functions: • taking arguments of type T • returning values of type T’ • Example: # let incr = fun x -> x + 1;; val incr : int -> int = <fun> # let incr x = x + 1;; val incr : int -> int = <fun> • Explicit type annotation # let inc = fun (x:int) -> x + 1;; val inc : int -> int = <fun> # let incr (x:int) = x + 1;; val incr : int -> int = <fun>

  49. x funx -> (fun y -> y+x) fun y -> y+x Type of add

  50. Type of add int funx -> (fun y -> y+x) fun y -> y+x

More Related