CS 11 Ocaml track: lecture 3

Similar documents
OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

C++ basics Getting started with, and Data Types.

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

A New Implementation of Formats based on GADTs

CSCI 2041: Functions, Mutation, and Arrays

CSE 303: Concepts and Tools for Software Development

CS 11 Ocaml track: lecture 2

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

Introduction to Objective Caml

Ruby: Introduction, Basics

Reference Cells. Example

Flow Control. CSC215 Lecture

CSCI-GA Scripting Languages

CS 11 Haskell track: lecture 1

C for C++ Programmers

Ruby: Introduction, Basics

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

G Programming Languages - Fall 2012

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Some Advanced ML Features

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

OCaml Language Choices CMSC 330: Organization of Programming Languages

CS102: Variables and Expressions

Abram Hindle Kitchener Waterloo Perl Monger October 19, 2006

An overview of Java, Data types and variables

Module Pervasives. Exceptions

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Programming Languages and Techniques (CIS120)

C programming basics T3-1 -

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Kurt Schmidt. October 30, 2018

Imperative programming in F#

Review for Test 1 (Chapter 1-5)

Python I. Some material adapted from Upenn cmpe391 slides and other sources

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS302: Self Check Quiz 2

Lecture 03 Bits, Bytes and Data Types

CS 11 python track: lecture 3. n Today: Useful coding idioms

CMSC 330: Organization of Programming Languages. Rust Basics

Introduction to Programming Using Java (98-388)

CS 11 C track: lecture 8

COSC2031 Software Tools Introduction to C

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Functions & First Class Function Values

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

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

The Warhol Language Reference Manual

Coral Programming Language Reference Manual

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

Dynamic Types, Concurrency, Type and effect system Section and Practice Problems Apr 24 27, 2018

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

FOFL and FOBS: First-Order Functions

CMSC 330: Organization of Programming Languages

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CS153: Compilers Lecture 15: Local Optimization

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

CSI 402 Lecture 2 Working with Files (Text and Binary)

Ruby: Objects and Dynamic Types

CS 139 Practice Midterm Questions #2

A Short Introduction to OCaml

4. Inputting data or messages to a function is called passing data to the function.

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Pace University. Fundamental Concepts of CS121 1

Input And Output of C++

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

8. Characters, Strings and Files

Binghamton University. CS-220 Spring Includes & Streams

Lecture 8: Structs & File I/O

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Static Program Analysis Part 1 the TIP language

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

COMP 202 Java in one week

Control Structures in Java if-else and switch

IO Monad (3D) Young Won Lim 1/18/18

Scala : an LLVM-targeted Scala compiler

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

A quick introduction to SML

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Mini-ML. CS 502 Lecture 2 8/28/08

CS313D: ADVANCED PROGRAMMING LANGUAGE

Using Scala in CS241

Contents. A Review of C language. Visual C Visual C++ 6.0

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CSE341, Fall 2011, Lecture 26 Summary

CS201- Introduction to Programming Current Quizzes

MIDTERM EXAMINATION - CS130 - Spring 2003

The SPL Programming Language Reference Manual

YOLOP Language Reference Manual

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

CS112 Lecture: Working with Numbers

Introduction to C An overview of the programming language C, syntax, data types and input/output

Ruby: Introduction, Basics

Programmazione Avanzata

CS 230 Programming Languages

CS 11 Ocaml track: lecture 7

Transcription:

CS 11 Ocaml track: lecture 3 n Today: n A (large) variety of odds and ends n Imperative programming in Ocaml

Equality/inequality operators n Two inequality operators: <> and!= n Two equality operators: = and == n Usually want to use = and <> n = means "structurally equal" n <> means "structurally unequal" n == means "the same exact object" n!= means "not the same exact object"

Unit type n The unit type is a type with only one member: () n not a tuple with only one element! n tuples must have at least two elements n Seems useless, but n all Ocaml functions must return a value n return () when value is irrelevant n i.e. when function called for side effects

Option types type 'a option = None Some of 'a n Built in to Ocaml n Used for functions that can return a value but can also "fail" (return None) n Alternative to raising exception on failure

String accessing/mutating (1) n Strings are not immutable n Can treat as an array of chars n To access a particular char: n s.[i] n To mutate a particular char: n s.[i] <- 'a'

String accessing/mutating (2) # let s = "some string" ;; val s : string = "some string" # s.[0] ;; (* note weird syntax *) - : char = 's' # s.[0] <- 't' ;; - : unit = () # s ;; - : string = "tome string"

String accessing/mutating (3) n String mutation is a misfeature! n n only in the language because of historical reasons most new languages have immutable strings n Ocaml is starting to move towards immutable strings by default n with a "bytes" type for when you want a mutable string-like type n The safe-string option turns off the ability to mutate strings

printf and friends (1) # Printf.printf "hello, world!\n" ;; hello, world! - : unit = () # open Printf ;; # printf "hello, world!\n" ;; hello, world! - : unit = ()

printf and friends (2) # printf "s = %s\tf = %f\ti = %d\n" "foo" 3.2 1 ;; s = foo f = 3.200000 i = 1 - : unit = () n printf has a weird type n n not really well-typed compiler "knows" about it and makes it work

printf and friends (3) # fprintf stderr "Oops! An error occurred!\n" ;; - : unit = () # stderr ;; - : out_channel = <abstr> n Predefined I/O "channels": n stdin : in_channel n stdout : out_channel n stderr : out_channel

printf and friends (4) # sprintf "%d + %d = %d\n" 2 2 4 ;; - : string = "2 + 2 = 4\n" n sprintf is "printing to a string" n Very useful!

File I/O (1) n Files come in two flavors: input and output # open_in ;; - : string -> in_channel = <fun> # open_out ;; - : string -> out_channel = <fun> # close_in ;; - : in_channel -> unit = <fun> # close_out ;; - : out_channel -> unit = <fun>

File I/O (2) n Files come in two flavors: input and output n let infile = open_in "foo" n tries to open file named "foo" for input only n binds file object to infile n close_in infile n closes the input file

File I/O (3) n Files come in two flavors: input and output n let outfile = open_out "bar" n tries to open file named "bar" for output only n binds file object to outfile n close_out outfile n closes the output file

File I/O (4) n flush stdout n forces an output file (here, stdout) to write its buffers to the disk n input_line stdin n gets a line of input from an input file (here, stdin) and returns a string

begin/end and sequencing (1) n With side effects, often want multiple statements inside a function: let print_and_square x = Printf.printf "%d\n" x ; x * x n Single semicolon used to separate statements that execute one after another

begin/end and sequencing (2) n Sometimes want to say "these sequences should be treated as a single expression" n Use begin/end for this: begin end Printf.printf "%d\n" x; x * x n Can often leave out begin/end

begin/end and sequencing (3) n Sometimes can just use parentheses: (Printf.printf "%d\n" x ; x * x) n I advise against this n Can make code hard to read

begin/end and sequencing (4) n Very often, when you get weird error messages it's because you should have put in a begin/end somewhere n Commonly found in nested match expressions (Ocaml grammar is highly ambiguous!) n When in doubt, add explicit begin/end statements everywhere you use sequencing

assert n Ocaml has an assert statement like most imperative languages n Not a function! n Takes one "argument", a boolean n If it's false, raises Assert_failure exception n Turn off assertions with -noassert compiler option

On to... n Imperative programming! n We've already done imperative programming n printf is a function called for side-effects only n begin/end and sequencing only useful for side effecting operations n Now want to cover the "core" of imperative programming

Imperative programming n Imperative data types: n references n records with mutable fields n mutable arrays n Imperative statements: n for loop n while loop n Breaking out of loops

References (1) n A reference type is like a "box" that holds a single value: # let x = ref 0 ;; val x : int ref = {contents = 0} #!x ;; - : int = 0

References (2) n The! operator fetches the value from the reference "box" n The := operator assigns a new value to the reference # x := 10 ;; - : unit = () # x ;; - : int ref = {contents = 10} n LHS of := must be a reference, not a value!

while loop n while loop is basically like C/C++/Java while loop: while <condition> do <stmt1>; <stmt2>;... <stmtn> done

Example let factorial n = let result = ref 1 in let i = ref n in while!i > 1 do result :=!result *!i; i :=!i - 1 done;!result n Very easy to accidentally omit! operators

Records with mutable fields (1) n References are just a special case of records with mutable fields n Recall record type declaration: type point = { x: int; y: int } n This declares point as an immutable type n x and y fields can't change after point created n not always what you want

Records with mutable fields (2) n To get mutable fields: type point = { mutable x: int; mutable y: int } n Now can change x, y fields: let p = { x = 10; y = 20 } ;; val p : point = {x = 10; y = 20} # p.x <- 1000 ;; - : unit = () # p ;; - : point = {x = 1000; y = 20}

Records with mutable fields (3) n To get only some mutable fields: type point = { x: int; mutable y: int } n Now can change only change y field: # let p = { x = 10; y = 20 } ;; val p : point = {x = 10; y = 20} # p.x <- 1000 ;; The record field label x is not mutable

Records with mutable fields (4) n n n The <- record mutation operator is not a true operator Just built-in syntax The! and := reference operators are true operators: # (!) ;; - : 'a ref -> 'a = <fun> # (:=) ;; - : 'a ref -> 'a -> unit = <fun>

Arrays n Recall: literal arrays: # let arr = [ 10; 20; 30; 40; 50 ] ;; n Arrays are always mutable: # arr.(0) ;; - : int = 10 # arr.(0) <- 1000 ;; (* same syntax as records *) - : unit = () # arr ;; - : int array = [ 1000; 20; 30; 40; 50 ]

for loops # for i = 1 to 10 do done; Printf.printf "%d " i Printf.printf "\n";; 1 2 3 4 5 6 7 8 9 10 - : unit = () n Index variable i assigned values from 1 to 10, inclusive n Don't need to use!i syntax to refer to i's value

Breaking out of loops n No break statement like in C/C++/Java n Instead, raise an Exit exception and catch it: # try for i = 1 to 10 do if i = 5 then raise Exit (* like a "break" *) else Printf.printf "%d " i done with Exit -> Printf.printf "\n";; 1 2 3 4 - : unit = ()

Next time n Modules n Functors