Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.

Slides:



Advertisements
Similar presentations
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Advertisements

Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
The Web Warrior Guide to Web Design Technologies
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Operators, Functions and Modules1 Pattern Matching & Recursion.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Computer Science 101 Introduction to Programming.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Fundamental Programming: Fundamental Programming Introduction to C++
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Introduction to Java Java Translation Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Expressions and Data Types Professor Robin Burke.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
A Sample Program #include using namespace std; int main(void) { cout
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
Variables, Expressions, and IO
Haskell.
Chapter 2 - Introduction to C Programming
Strings Part 1 Taken from notes by Dr. Neil Moore
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Primitive Data types
Chapter 2 - Introduction to C Programming
Python Primer 1: Types and Operators
Chapter 2: Introduction to C++.
Chapter 2 - Introduction to C Programming
Primitive Types and Expressions
Unit 3: Variables in Java
Introduction to C Programming
Introduction to Primitive Data types
Presentation transcript:

Introduction to Objective Caml

General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of ML –Standard ML –Caml (including Objective Caml, or OCaml) ML is interactive ML is case-sensitive ML is strongly typed

Using Objective Caml

Programs on files Save your program file with a.ml extension –As always, use a decent text editor To load a program: –Choose File -> Open... –Click OK (to clear the input pane) –Navigate to your file and open it –Hit the Return key in the terminal input window

Two kinds of Enter! The Enter key on the main keyboard sends your expression to Ocaml to be executed The Enter key on the numeric keypad just gives you a new line in the input pane If you copy and paste code from another source, you may get mysterious errors –Replace each newline with the keypad kind!

Comments Comments are enclosed in (* and *) Comments can be nested Nested comments are useful for commenting out a block of code that may itself contain comments

Identifiers Identifiers are composed of letters, digits, underscores ( _ ), and primes ( ' ) –Normal variables must start with a lowercase letter –"prime" = "apostrophe" = "single quote" = "tick" –Only "type variables" can start with a prime –An underscore all by itself, _, is a wildcard

Primitive types int 0, 5, 42, -17, 0x00FF –Negative int s often need to be parenthized: (-1) float 0.0, -5.3, 1.7e14, 1e-10 –Cannot start with a decimal point bool true, false string "", "One\nTwo\nThree" char 'a', '\n'

Trying things at the prompt End expressions with a double semicolon, ;; # 2 + 2;; - : int = 4 # let five = 5;; val five : int = 5 # 2 + five;; - : int = 7 Caml shows you the type because it's important

Operations on bool s Comparisons give a bool result >= > Standard operators are not, &&, || # (1 < 3) && (3 <= 5);; - : bool = true && and || are short-circuit operators There is an and, but it's something else entirely

Arithmetic Arithmetic on integers: + - * / 2 + 2;; # - : int = 4 Arithmetic on floats: *. /. # ;; - : float = # ;; # Characters 6-9: This expression has type float but is here used with type int

No mixed-mode arithmetic # ;; # Characters 4-7: This expression has type float but is here used with type int # ;; # Characters 0-1: This expression has type int but is here used with type float

Numeric coercions #float 5;; - : float = # truncate 3.8;; - : int = 3 # floor 3.8;; - : float = # ceil 3.8;; - : float =

Comparisons Comparisions >= > can be applied to any type –Comparisons have their usual meanings for primitive types ( int, float, bool, string, char ) –Their meaning is undefined for more complex types The comparisons == and != test whether two things occupy the same storage locations

Operations on strings and chars ^ is string concatenation String.length s returns the size of string s String.index s c finds character c in string s String.sub s p n returns a substring of s of length n starting from p String.uppercase s uppercases all letters in s String.capitalize s uppercases the first character in s

Lists Lists are semicolon-separated and enclosed in brackets # [3; 5; 7];; - : int list = [3; 5; 7] All elements of a list must be the same type –Note that the type above is given as int list

The empty list The empty list is represented by [ ] The empty list has a type When Caml doesn't know the type, it represents it with a type variable 'a, 'b, 'c,... # [ ];; - : 'a list = [] Sometimes Caml does know the type of [ ] !

Operations on lists hd returns the head ( CAR ) of a list tl returns the tail ( CDR ) of a list :: adds an element to a list ( CONS ) –Example: 1 :: [3; 5; 7] gives [1; 3; 5; appends two lists ( APPEND ) List.length l returns the length of l List.nth l n returns the n th element of l

Tuples Elements of a tuple are separated by commas Tuples are usually enclosed in parentheses # ("pi", , [1;2;3]);; - : string * float * int list = "pi", , [1; 2; 3] Notice the type: (string * float * int list) [(3, 5.0); (3.0, 5)] isn't legal--why not?

Tuples of different sizes Notice the types in the following: # (1, 2, 3);; - : int * int * int = 1, 2, 3 # (1, 2);; - : int * int = 1, 2 # (1);; - : int = 1 (* Why is this an int? *) For x of any type, (x) is the same as x

Operations on tuples A pair is a tuple of two elements fst t is the first element of a pair t snd t is the second element of a pair t That's all! You can define additional operations on tuples by using pattern matching (more about that later)

The unit The empty tuple is represented as ( ) The empty tuple is called the "unit" There is (obviously) only one value of this type, but it is a value There are a (very) few places where we use the unit because we need to provide a value but don't care what it is.

Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = # add (3, 5);; - : int = 8 Notice the type of add: int * int -> int = The -> indicates that this is a function The value (function body) is abbreviated to

The End