This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1 CMSC 330: Organization of Programming Languages OCaml 1 Functional Programming CMSC 330 2 Dialects of ML ML (Meta Language) Univ. of Edinburgh,1973 Part of a theorem proving system LCF & The Logic of Computable Functions SML/NJ (Standard ML of New Jersey) Bell Labs and Princeton, 1990 Now Yale, AT&T Research, Univ. of Chicago, etc OCaml (Objective CAML) INRIA, 1996 French Natl Institute for Research in Computer Science CMSC 330 3 Dialects of ML (cont.) Other dialects MoscowML, ML Kit, Concurrent ML, etc SML/NJ and OCaml are most popular Languages all have the same core ideas But small and annoying syntactic differences So you should not buy a book with ML in the title & Because it probably wont cover OCaml CMSC 330 4 Features of ML Mostly functional Some assignments Higher-order functions Functions can be parameters and return values Type inference No need to write types in the source language & But the language is statically typed Supports parametric polymorphism & Generics in Java, templates in C++ CMSC 330 5 Features of ML (cont.) Data types and pattern matching Convenient for certain kinds of data structures Exceptions Garbage collection CMSC 330 6 Functional Languages In a pure functional language Every program is just an expression evaluation let add1 x = x + 1;; let rec add (x,y) = if x=0 then y else add(x-1, add1(y));; add(2,3) = add(1,add1(3)) = add(0,add1(add1(3))) = add1(add1(3)) = add1(3+1) = 3+1+1 = 5 2 CMSC 330 7 Functional Languages (cont.) OCaml has similar basic behavior Program = expression evaluation But has additional features To ease the programming process Features support & Less emphasis on data storage & More emphasis on function execution CMSC 330 8 A Small OCaml Program Things to Notice (* Small OCaml program *) let x = 37;; let y = x + 5;; print_int y;; print_string "\n";; Use (* *) for comments (may nest) ;; ends a top-level expression Use let to bind variables No type declarations Need to use correct print function (OCaml also has printf) Line breaks, spacing ignored (like C, C++, Java, not like Ruby) CMSC 330 9 OCaml Interpreter Expressions can be typed and evaluated at the top-level # 3 + 4;;- : int = 7 # let x = 37;; val x : int = 37 # x;;- : int = 37 # let y = 5;; val y : int = 5 # let z = 5 + x;; val z : int = 42 # print_int z;; 42- : unit = () # print_string "Colorless green ideas sleep furiously";; Colorless green ideas sleep furiously- : unit = () # print_int "Colorless green ideas sleep furiously";; This expression has type string but is here used with type int gives type and value of each expr unit = no interesting value (like void) - = the expression you just typed CMSC 330 10 OCaml Interpreter (cont.) Files can be loaded at top level % ocaml Objective Caml version 3.08.3 # #use "ocaml1.ml";; val x : int = 37 val y : int = 42 42- : unit = ()...
View Full Document
- Fall '08