J-O-Caml (3) pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 26.

Slides:



Advertisements
Similar presentations
Basic Java Constructs and Data Types – Nuts and Bolts
Advertisements

Introduction to C Programming
Pointers.
Lectures 10 & 11.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Intermediate Code Generation
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Programming Languages and Paradigms
Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Kernighan/Ritchie: Kelley/Pohl:
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
VBA Modules, Functions, Variables, and Constants
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Computer Science II Exam I Review Monday, February 6, 2006.
Chapter 8 Arrays and Strings
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Data Types.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 Programming Language History and Evolution In Text: Chapter 2.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Programming Languages and Paradigms Imperative Programming.
J-O-Caml (6) pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, December 4.
J-O-Caml (2) pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 20.
Cs776 (Prasad)L16rem1 Remaining Features of SML97 Records Exceptions Reference Types Arrays.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
J-O-Caml (1) Qinghua, November 19.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Introduction to C# By: Abir Ghattas Michel Barakat.
Characters and Strings
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 CSE1301 Computer Programming: Where are we now in the CSE1301 syllabus?
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CMSC 330: Organization of Programming Languages Object Oriented Programming with OCaml.
Programming Language History and Evolution
The Machine Model Memory
Programming Language History and Evolution
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
C Pointers Another ref:
SPL – PS2 C++ Memory Handling.
Presentation transcript:

J-O-Caml (3) pauillac.inria.fr/~levy/qinghua/j-o-caml Qinghua, November 26

Plan of this class records references and mutable data input/output exceptions a tour in library modules and interfaces labeling algorithm

Exercices Conway sequences - solution 1 Conway sequences - solution 2 (with less many conses) ?

Zero-ary functions functions are monadic in Caml type constructors (which are not functions) have arity (maybe 0)

Records type ``record’’ needs be declared

Records type ``record’’ needs be declared

Mutable fields in records several fields may be declared mutable in records ( students in previous example) until now, all variables were constant important information for garbage collector, parallel evaluator, caches, etc constant values are less error-prone than mutable values, especially with sharing, concurrency, etc. in C, C++, Java, etc, variables are mutable by default in ML, it’s the opposite Keeping variables constant is the basis of Functional Programming (no side-effects) In Haskell, mutable world (monads) and constant world (usual expressions) are distinct.

References ref v is L-value of the mutable value v (a pointer address!) !x dereferences x and produces v := modifies the value of a reference (Beware: := for references; <- for arrays and strings!!) a reference is equivalent to a record with a single mutable field contents v ref v

Imperative programming with references, records, strings and arrays, one can use the imperative style of C, C++, Java, etc. however dereferencing of references must be explicit (no R-values)

Imperative programming sorting arrays (a la Sedgewick)

Exceptions There are several built-in exceptions Failure, Division_by_zero, Invalid_argument, etc but exceptions may also be declared by: raise and try... with... handle exceptions with pattern-matching try e with | exception_1 -> e_1 | exception_2 -> e_2... | exception_n -> e_n

Input/Output

Modules modules group functions of same nature qualified notation Array.make, List.length as in Java, Modula, etc they can be opened as in open Printf module Pervasives always open fetch modules in documentation at caml.inria.fr/pub/docs/manual-ocaml module Graphics is a portable graphics library (needs graphics.cma to be compiled as first argument of the ocamlc command) module names ( List ) start with uppercase letter and correspond to interfaces ( list.cmi ) starting with lowercase letter.

Graphics ocamlc graphics.cma g1.ml a.out

Graphics elementary functions moveto, lineto, draw_rect, fill_rect,... type color is int images are internal representation of bitmaps a matrix of colors can be made into an image make_image an image can be displayes dump_image

Combien d’objets dans une image? Jean-Jacques Lévy INRIA

Labeling 16 objects in this picture

Algorithm 1) first pass - scan pixels left-to-right, top-to-bottom giving a new object id each time a new object is met second pass - generate equivalences between ids due to new adjacent relations met during scan of pixels. third pass - compute the number of equivalence classes Complexity: - scan twice full image (linear cost) - try to efficiently manage equivalence classes (Union-Find by Tarjan)