Introduction to Functional Programming in Ocaml

Introduction to Functional Programming in Ocaml

Introduction to Functional Programming in OCaml Roberto Di Cosmo, Yann Régis-Gianas, Ralf Treinen Week 4 - Sequence 2: Functions With Several Arguments Expressions for Multi-Argument Functions I An anonymous function with several arguments is written fun p1 ... pn -> exp where the pi are patterns I Unlike function, the fun form only admits one case (or branch), for example, fun (x,y) 1 -> x | (x,y) 2 -> y is not accepted 2 Functions Returning Functions I Functions are First-Class Values I The return value of a function may be a function: function n -> (function x -> x+n) I The type of this function is something we have seen earlier! 3 Functions Returning FunctionsI let f1 = function n -> (function x -> n+x);; # val f1 : int -> int -> int = <fun> (f1 17) 73;; # - : int = 90 f1 17 73;; # - : int = 90 let f2 = fun n x -> n+x;; # val f2 : int -> int -> int = <fun> f2 17 73;; # - : int = 90 4 Functions Returning Functions II (f2 17) 73;; # - : int = 90 5 The Truth About Functions With Multiple Arguments I Functions with multiple arguments are the same thing as functions returning functions as values! I More precisely: fun x1 ... xn -> e is just an abbreviation for function x1 -> ... -> function xn -> e 6 Four equivalent function definitionsI type expr = | Var of string | Add of expr * expr;; # type expr = Var of string | Add of expr * expr let rec eval = fun environment expr -> match expr with | Var x -> List.assoc x environment | Add(e1,e2) -> (eval environment e1) + (eval environment e2);; # val eval : (string * int) list -> expr -> int = <fun> eval [("x",2); ("y",5)] (Add (Var "x", Add (Var "x", Var "y")));; # - : int = 9 7 Four equivalent function definitions II let rec eval = function environment -> function expr -> match expr with | Var x -> List.assoc x environment | Add(e1,e2) -> (eval environment e1) + (eval environment e2);; # val eval : (string * int) list -> expr -> int = <fun> eval [("x",2); ("y",5)] (Add (Var "x", Add (Var "x", Var "y")));; # - : int = 9 8 Four equivalent function definitions III let rec eval = function environment -> function | Var x -> List.assoc x environment | Add(e1,e2) -> (eval environment e1) + (eval environment e2);; # val eval : (string * int) list -> expr -> int = <fun> eval [("x",2); ("y",5)] (Add (Var "x", Add (Var "x", Var "y")));; # - : int = 9 9 Four equivalent function definitionsIV let rec eval environment = function | Var x -> List.assoc x environment | Add(e1,e2) -> (eval environment e1) + (eval environment e2);; # val eval : (string * int) list -> expr -> int = <fun> eval [("x",2); ("y",5)] (Add (Var "x", Add (Var "x", Var "y")));; # - : int = 9 10.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us