1 / 19

Functional Programming Language OCaml Tutorial

Functional Programming Language OCaml Tutorial. 科大 - 耶鲁联合研究中心 http://kdyhcs.ustcsz.edu.cn. Outline. Introduction Get Started OCaml Basis Basic Concepts Examples Discussion. Introduction. Functional programming(FP) emphasizes application of functions and has its roots in lambda calculus

erwin
Download Presentation

Functional Programming Language OCaml Tutorial

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. Functional Programming LanguageOCaml Tutorial 科大-耶鲁联合研究中心 http://kdyhcs.ustcsz.edu.cn

  2. Outline Introduction Get Started OCaml Basis Basic Concepts Examples Discussion

  3. Introduction • Functional programming(FP) emphasizes application of functions and has its roots in lambda calculus • The difference between function in FP and imperative languages is side effects • FP can also be accomplished in imperative languages.

  4. History • 1950s LISP • 1960s APL -> J -> K • 1970s ML -> Objective Caml , Standard ML • 1980s Dependent Types -> Coq, Agda, Epigram • 1987 Haskell

  5. Get Started • Install • http://caml.inria.fr/pub/distrib/ocaml-3.11/ocaml-3.11.0-win-msvc.exe • Interactive • Run “ocaml” • Objective Caml version 3.11.2 • # 1+2;; • - : int =3 • Use any text editor • Emacs/Vim • Notepad++ …

  6. Get Started • Run Emacs • Visist New File -> “test.ml” • Short Keys • C-c C-s “start ocaml” • C-c C-b “eval whole file” • C-c C-r “eval selected code” • Tab “Ident” • Demo

  7. OCaml • Declaring Variables • Variables in OCaml are immutable! • Declaring Functions • OCaml functions don’t have explicit “return”. The return value is the last statement.

  8. OCaml • All functions and values in OCaml have a datatype • let add x y = (x + y);; • OCaml will report • val add : int -> int -> int = <fun> • which means • function “add” takes two “int” inputs and return a “int” • Generic Function • let fst x y = x • - val fst : 'a -> 'b -> 'a = <fun> • # fst 1 2 ;; • # fst "a" "b";; • # fst "a" 1;;

  9. Recursion and Nested Function

  10. Pattern Match • Example • Patterns can be chained together

  11. High-order Functions • A high-order function is a functions that takes another function as a parameter or a function returns another function or both. • Function can be partial applied • add : int -> int -> int • add 1 : int -> int • add 1 2 : int • let f = add 1 • let result = f 2

  12. High-order Functions • Composition • let double x = x * 2 • let power x = x * x • let compose f g x = g (f x) • let result = (compose double power) 4 • Pipeline • let pipe x f = f x • let result = (pipe (pipe 4 double) power)

  13. Anonymous Function • Example • fun x -> x * 2 • We can rewrite examples from Page 11 • compose (fun x->x*2) (fun x->x*x) 4 • Much use of anonymous functions

  14. Data Structures • Option Type • Tuples • List

  15. Examples • Fibonacci number • List filter, map, fold

  16. Lab • Write a calculator • Provided parser combinators • built-in parsers • integer, alpha, ident, char, string • combinators • many, sepBy, paren, chainl • infix • >> >>= <|> <?> • #use “parsec.ml”;;

  17. Lab • Grammar • Exp :: Term [+-] Term • Term :: Factor [*/] Factor • Factor :: int | ( Exp ) • Get Start • let add = (char ‘+’) >> (return (+)) <?> “+” • Receive a char ‘+’ and return function (+) with possible error message “+” • let factor = (paren exp) <|> (integer <?> “integer”) • either a parened “exp” or an integer • let term = chainl factor (mul <|> div) • a list of factor chained with “mul” or “div” • let expr = chainl term (add <|> sub)

  18. Lab • Write a parser for first-order logic formula • Formula :: Clause \/ Clause • Clause :: Term /\ Term • Term :: Literal | Literal Comp Literal • Literal :: id | int | bool • Eval the formula with given environment • (x:true;y:false;….)

  19. TODO • Spec# • ESC/Java • Compcert • CComp

More Related