OutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsCS421 Lecture 2: Introduction to OCaml1Mark [email protected] of Illinois at Urbana-ChampaignMay 31, 20061Based on slides by Mattox Beckman, as updated by Vikram Adve, GulAgha, and Elsa GunterMark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsMark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsToday’s ObjectivesYour goal in today’s lecture is to gain some familiarity withOCaml. In particular, you should know. . .Ihow to use immediate mode for interactive programming.Ithe basic builtin types.Ihow to use pattern matching in your functions.Ihow to use tuples and lists.Iknow the four ways to create variables, and how long they last.Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsOCamlIThe OCaml system in installed on the EWS machinesICode for the examples run in this lecture is available on thelectures pageMark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsOCaml OnlineIMain CAML home: http://caml.inria.fr/index.en.htmlITo install OCAML on your com puter se e:http://caml.inria.fr/ocaml/release.en.htmlICurrent release (as of May 27): 3.09.2Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsOCaml ReferencesIThe Objective Caml system release 3.09, by Xavier Leroy,online manualIDeveloping Applications With Objective Caml, byEmmanuel Chailloux, Pascal Manoury, and Bruno Pagano,published by OReilly, available online from course resourcesMark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsSome History...ICAML is European descendant of original MLIAmerican/British version is SMLIO is for object-oriented extensionIML stands for Meta-LanguageIML family designed for implementing theorem proversIIt was the meta-language for programming the object languageof the theorem proverIDespite obscure original application area, OCAML is a fullgeneral-purp os e programming languageMark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsFeatures of OCamlLanguage Features:Ihigher order functional languageIcall-by-value parameter passing styleImodern syntaxIparametric polymorphism (aka structural polymorphism)Iautomatic garbage collectionIuser-defined algebraic data typesIstrongly typed, uses type inferenceIIt’s very fast — the winners of the 2000 and 1999 ICFPProgramming Contests used OCaml.Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsWhy OCaml?IMany features not clearly in languages you have alreadylearnedIAssumed basis for much research in programming languagesIParticularly e fficient for programming tasks involvinglanguages (e g parsing, compilers, user interfaces)IMany real-world uses (SLAM at Microsoft, FFTW foroptimized FFT generation, etc)Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsStarting OCaml Immediate ModeIImmediate Mode statements must be terminated by a ;;ITypes are inferred if not explicitly providedIOutput can refer to either values returned by expressions ortext printed by print calls – we will usually mean the former1 $ ocaml2 Objective Caml version 3.09.234 # 34 + 8;;5 - : int = 426 # 27.0 +. 9.4;;7 - : float = 36.48 # "hello";;9 - : string = "hello"Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsHello, World!IThis is the Standard First ProgramTMIThe type unit is like void in C/C++; it representscommands.1 # print_string "Hello, world!\n";;2 Hello, world!3 - : unit = ()IThe type unit has one value, () (pronounced “unit”).INote:I“Hello, world!” has been output (I/O) to the screen, butIthe re sult of this function (the value returned) is ().Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsBasic Types1 # 20.3;;2 - : float = 20.33 # let answer = 42;;4 val answer : int = 425 # (answer < 50);;6 - : bool = true7 # "Ravi";;8 - : string = "Ravi"9 #Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsType ErrorsIf you try to combine things with incompatible types, you get atype error.1 # 1 + "hello";;2 ^^^^^^^3 This expression has type string but is here used4 with type int5 # 2.0 +. 4;;6 ^7 This expression has type int but is here used8 with type float9 #Mark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsEnvironmentsEnvironments play a central role in languages.ρ = {name17→ value1; name27→ value2; · · · }IMathematically, an environment is a partial functionITechnically a set, but can be represented as a list (find firstmatch) or a stackIThe environment is usually represented by letter ρ.IThe 7→ symbol is pronounced “maps to”.IEvaluating an expression always requires an environmentISome operations create a new environment from an old one:Iglobal let, local letIfunction call (invocation, not definition!)Imatch/withMark Hills CS421 Lecture 2: Introduction to OCamlOutlineObjectives for TodayIntroducing OCamlWriting OCaml ProgramsRunning OCamlTypesEnvironmentsPatternsListsOther SyntaxActivity SolutionsVariable Creation 1: Global Letlet name = body ;;1 # let a=42;; // ρ = { a 7→ 42 }2 val a : int = 423 # a * 6;;4 - : int = 2525 # let i=20;; // ρ = { i 7→ 20; a 7→ 42}6 val i : int = 207 # let a=10;; // ρ = { a 7→ 10; i 7→ 20}8
View Full Document