L3 Programming September 19, OCaml Cheatsheet

Similar documents
CSCI-GA Scripting Languages

Introduction to OCaml

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Exercises on ML. Programming Languages. Chanseok Oh

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Metaprogramming assignment 3

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

List Functions, and Higher-Order Functions

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Introduction to Objective Caml

A quick introduction to SML

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Homework 3 COSE212, Fall 2018

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS 11 Ocaml track: lecture 2

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Tracing Ambiguity in GADT Type Inference

ML Type Inference and Unification. Arlen Cox

Begin at the beginning

CSCI 2041: Functions, Mutation, and Arrays

Introduction to OCaml

Datatype declarations

Specification and Verification in Higher Order Logic

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Chapter 11 :: Functional Languages

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Handout 2 August 25, 2008

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CMSC 330: Organization of Programming Languages. Operational Semantics

Documentation for LISP in BASIC

Recap: Functions as first-class values

GADTs meet Subtyping

Programming Languages

CSE 130 [Winter 2014] Programming Languages

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Standard ML. Curried Functions. ML Curried Functions.1

Lists. Michael P. Fourman. February 2, 2010

Type Checking and Type Inference

CSE 341 Section 5. Winter 2018

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

SOFTWARE VERIFICATION AND COMPUTER PROOF (lesson 1) Enrico Tassi Inria Sophia-Antipolis

The Substitution Model. Nate Foster Spring 2018

Introduction to SML Getting Started

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Part I. Language Core

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Option Values, Arrays, Sequences, and Lazy Evaluation

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Ruby: Introduction, Basics

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Organization of Programming Languages CS3200/5200N. Lecture 11

Fall Recursion and induction. Stephen Brookes. Lecture 4

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Programming Languages Lecture 15: Recursive Types & Subtyping

Functional Programming. Pure Functional Programming

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CSE341, Spring 2013, Final Examination June 13, 2013

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Lecture 2: SML Basics

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

Problem Set CVO 103, Spring 2018

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

The reader is referred to the Objective Caml reference manual for a more detailed description of these features.

Tomas Petricek. F# Language Overview

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

CMSC 330: Organization of Programming Languages

Programming Languages

Syntax and Grammars 1 / 21

CSE341 Spring 2017, Final Examination June 8, 2017

COMP 105 Homework: Type Systems

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

15 150: Principles of Functional Programming. More about Higher-Order Functions

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Lecture #23: Conversion and Type Inference

The Substitution Model

CSE341 Spring 2016, Final Examination June 6, 2016

Abram Hindle Kitchener Waterloo Perl Monger October 19, 2006

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

CS205: Scalable Software Systems

Transcription:

OCaml Cheatsheet Note: this document comes from a previous course (by Sylvain Schimdt). The explanations of the OCaml syntax in this sheet are by no means intended to be complete or even sufficient; check http://mirror.ocamlcore.org/ocaml-tutorial. org/ for further information. 1 The let Keyword, Functions Defining constants and functions in Caml is done through the let keyword. let x = 2;; defines a constant x with value 2. let double a = 2 a;; defines a function double that returns the double of its parameter. Another possible syntax is let double = fun a 2 a;;. let x = value in expr ;; binds x to value, but only in expr. 2 The OCaml interpreter The command ocaml (without parameters) launches the Caml interpreter. You can type Caml instructions inside and immediately get their result it is highly recommended to use the ledit command with ocaml as argument to make interacting with this interpreter bearable. For instance, you can type: # l e t x = 2 ; ; val x : i n t = 2 # l e t double = fun a 2 a ; ; val double : i n t i n t = fun The shell returns information about the newly defined object in the form val name : type = value. Function types are of the form type param type result. You may want to import a source file in the interpreter. You can do so by using the command #use file. ml ;;. You can also directly run a file outside the interpreter by: ocaml file.ml. If you are using an external module, you might need to provide the ocaml command with a path, e.g. ocaml -I +labltk to add the LablTK library. 3 Polymorphism, Pairs Let us look at the identity function: # l e t id = fun x x val id : a a = fun 1

This is a polymorphic function. It can take any type as input, and returns a value of the same type. Given x and y, a pair can be built by using the notation (x, y). If x is of type a and y of type b, then (x, y) is of product type a b. Exercise 1. Write a function dup of type a ( a a) that, given a value, returns a pair with the input value as both first and second elements. 4 Curryfication Let us redefine addition: # l e t a d d i t i o n x y = x + y ; ; val a d d i t i o n : i n t i n t i n t = fun # a d d i t i o n 2 ; ; : i n t i n t = fun Defined like this, addition is a function of type int (int int), that is, a function that takes an integer and returns a function that takes an integer and returns an integer. This is called curryfication. This is the preferred way of defining functions with more than one parameter (instead of using pairs). Exercise 2. Write a function curry of type (( a b) c) a b c, that, given a function that uses a pair to encode parameters, returns a currified function. Exercise 3. Write a function decurry of type ( a b c) ( a b) c that performs the opposite operation. Exercise 4. The function List.map of type ( a b) a list b list takes a function and a list and returns the list obtained by applying the function to each element of the initial list. Write a function add1 that takes a list and adds 1 to each element of this list. 5 Recursion If you want to define a recursive function, then the rec keyword must be added: # l e t f x = i f x = 0 then 0 else (1 + f ( x 1 ) ) ; ; Error : Unbound value f # l e t rec f x = i f x=0 then 0 else (1 + f ( x 1 ) ) ; ; val f : i n t i n t = fun Exercise 5. Write a function fibonacci such that fibonacci n returns the nth term of the Fibonacci sequence. Exercise 6. Test your Fibonacci function with n = 5, then n = 400. If this does not terminate within a reasonable time, improve your function! 2

6 Sum Types, Pattern Matching A sum type can be defined by the following syntax: # type sumtype = FirstCase [ of type ] > SecondCase [ of type ]... > LastCase [ of type ] This can be recursive. For example, Caml lists could be defined by: # type a l i s t = Nil Cons of a a l i s t Then, you could build a list by using the syntax Cons(1, Cons(2, Nil)). Exercise 7. Define a type that can represent propositional formulæ with the Not, And, and Or connectives and a Var constructor for variable names (using strings for names). The interest of sum types lies with pattern matching. constructions to filter x based on its type: # match ( x ) with > FirstCase [ ( v1 ) ]... >... > LastCase [ ( vn ) ]... You can use the following An extremely useful construction is function x, which is syntactic sugar for fun x match x with. Exercise 8. A propositional formula is in negative normal form if the negation operator is only applied to propositions. Define a fonction nnf that turns a propositional formula into an equivalent formula in negative normal form. Exercise 9. A litteral is either a variable or the negation of a variable. A propositional formula is in conjunctive normal form if it is of the form: (where l i,j are litterals) i j l i,j Define a function cnf that turns a propositional formula into an equivalent expression in conjunctive normal form. Exercise 10. A list of variable assignements can be given in the following form (this uses the Caml notation for lists): # l e t a s s o c = [ ( x, true ) ; ( y, f a l s e ) ; ( z, true ) ] ; ; val a s s o c : ( s t r i n g bool ) l i s t = val Define a function eval of type ( string bool) list propform bool that evaluates a propositional formula given a variable assignement. You might want to use the function List. assoc; check its documentation using man List. Exercise 11. Define a function fv that, given a propositional formula, returns the list of variables that appear inside, without duplicates. 3

Exercise 12. Define a function compile that, given a propositional formula, prints on the standard output a Caml function that has the behaviour of the formula. For example, you should be able to have something like: # compile (And(Or( Var x, Var y ), Or( Var x, Var z ) ) ) ; ; fun x y z ( ( x ) ( y ) ) && ( ( x ) ( z ) ) : unit 7 References Up to now, we only used constants, and never mutable variables. So, how can we modify a variable value? A possible answer could be that you should never use mutable variables in Caml, but their use can sometimes be justified. To define a mutable variable (called a reference), do: # l e t x = ref ( 0 ) ; ; val x : i n t ref = c o n t e n t s = 0 You can assign and retrieve the value of a reference by using := and!: # x := 1 ; ; #! x ; ; i n t = 1 Exercise 13. Translate the following functions into Caml by trying to stick as close as possible to C style. int f a c t o r i e l l e ( int n ) int y = 1, r e s = 1 ; while ( y <= n ) r e s = y ; y++; return r e s ; int findmax ( int [ ] array, int length ) int max, i = 1 ; a s s e r t ( l e n g t h > 0 ) ; max = array [ 0 ] ; while ( i < l e n g t h ) i f ( array [ i ] > max) max = array [ i ] ; 4

i ++; return max ; Exercise 14. Rewrite these functions in Caml style (use a list instead of an array for findmax). 5