CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

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

SML A F unctional Functional Language Language Lecture 19

A quick introduction to SML

Note that pcall can be implemented using futures. That is, instead of. we can use

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

F28PL1 Programming Languages. Lecture 11: Standard ML 1

A Brief Introduction to Standard ML

Exercises on ML. Programming Languages. Chanseok Oh

Advanced Algorithms and Computational Models (module A)

Product types add could take a single argument of the product type int * int fun add(,y):int = +y; > val add = fn : int * int -> int add(3,4); > val i

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

DaMPL. Language Reference Manual. Henrique Grando

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Handout 2 August 25, 2008

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Functional Programming

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

C: How to Program. Week /Mar/05

L-System Fractal Generator: Language Reference Manual

Introduc)on To Standard ML

Chapter 17. Fundamental Concepts Expressed in JavaScript

A LISP Interpreter in ML

SPARK-PL: Introduction

Chapter 2 SML, a Functional Programming Language

2.1. Expressions. Chapter 2 SML, a Functional Programming Language. Integers. Interacting with ML

cs1114 REVIEW of details test closed laptop period

C++ Programming: From Problem Analysis to Program Design, Third Edition

Operators. Java operators are classified into three categories:

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

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

Operators And Expressions

List Processing in SML

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

Chapter 2 - Introduction to C Programming

CS 320: Concepts of Programming Languages

2.1. Expressions. Chapter 2 ML, a Functional Programming Language. Integers. Interacting with ML

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Programming Paradigms Languages F28PL, Lecture 1

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

Online Resource. Online introductory book on programming in SML (Standard ML): online.pdf

The type checker will complain that the two branches have different types, one is string and the other is int

List Processing in SML

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Lecture Notes on Induction and Recursion

PLT Fall Shoo. Language Reference Manual

The SPL Programming Language Reference Manual

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Language Reference Manual simplicity

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

Homework 3 COSE212, Fall 2018

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Begin at the beginning

Data Types The ML Type System

CSE 341 Section 5. Winter 2018

Introduction to TURING

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

LECTURE 17. Expressions and Assignment

EXPRESSIONS AND ASSIGNMENT CITS1001

Chapter 15. Functional Programming Languages

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

(Not Quite) Minijava

VARIABLES & ASSIGNMENTS

Course PJL. Arithmetic Operations

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Introduction to Programming, Aug-Dec 2008

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

Compiler Techniques MN1 The nano-c Language

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

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Chapter 2 Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

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

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Products and Records

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

abstype 'a queue = q of 'a list with val Null = q([]) fun front(q(h::_)) = h fun rm(q(_::t)) = q(t) fun enter(v,q(l)) = q(rev(v::rev(l))) end

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

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

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Lecture 1. Types, Expressions, & Variables

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Functional Programming Languages (FPL)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

GAWK Language Reference Manual

Functional Programming

MIT AITI Python Software Development

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Lecture 2: The Basis of SML

JME Language Reference Manual

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

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

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

Chapter 2: Using Data

Transcription:

CS109A ML Notes for the Week of 1/16/96 Using ML ML can be used as an interactive language. We shall use a version running under UNIX, called SML/NJ or \Standard ML of New Jersey." You can get SML/NJ by the command sml on the \elaine's." It is also possible to run a program without interaction. Put the program in a le, e.g. foo and issue the UNIX command sml <foo Example: Here is an example of an interaction. Human-typed things are in the teletype font; things typed by the machine are in italic font. sml Standard ML of New Jersey, Version 0.93, February 15, 1993 val it = () : unit 5; val it = 5 : int "abc"; val it = "abc" : string <ctrl>d (machine returns to UNIX command level) The normal response of SML/NJ is val (short for \value"), followed by it (a special identi- er that means \the previously typed expression"), an equal-sign, the value of the expression, a colon, and the type of the expression. Special case: the rst response says that it has the value (), and its type is unit. The unit is a special \null type," whose only possible value is (). We then type the expression 5, followed by a semicolon. ML responds that the value of this expression is 5 and it is an integer. Semicolon must end all expressions. 1

SML/NJ gives you a \{" prompt when it is ready to begin an expression and an \=" prompt if it is waiting for you to complete an expression. Often an unexpected \=" means you have forgotten the semicolon. We type "abc", and ML tells us this expression is a string with value "abc". Variables in ML An ML program operates in a workspace of variables, much like a C program. We can assign a value to variables foo and bar by val foo = 5; val foo = 5 : int val bar = 7; val bar = 7 : int Remember to use \val" as if saying \the value of foo is 5." ML tells the value of the variable, not \it." We can use variables in expressions, as in other languages. ML evaluates any expression it is given. foo + bar; val it = 12 : int Arithmetic Operators Usual +, {, *, /. But / is for reals; use div for integers. mod gives the remainder of integers. ~ denotes unary minus. 2

Example: 4.0+5.0; val it = 9.0 : real 30 div 7; val it = 4 : int 30 mod 7; val it = 2 : int ~3*(~4); val it = 12 : int Note that parens (or a space) are needed for the last example. ML would interpret ~3*~4 as if *~ were a single operator and complain that it had never heard of that operator. Concatenation of Strings Operator ^ denotes concatenation of strings. "foo" ^ "bar" val it = "foobar" : string Comparison Operators As in C, but!= ) <> and == ) =. 4<=3; val it = false : bool "love" < "war"; val it = true : bool Note comparison of strings is lexicographic (dictionary) order. Type bool (Boolean) is the type of the result of a comparison. This type has only the two values: true and false. Logical Operators && ) andalso; ) orelse;! ) not. 3<4 andalso 5<4; val it = false : bool 3<4 andalso (not (4<5) orelse 5<6); val it = true : bool 3

Precedences of logical operators relative to each other and to the arithmetic or comparison operators are as in C, with one exception (not made clear in the book): not has higher precedence than any inx operator. Thus, the parens in \not (4<5)" are essential. Without them, ML tries to apply not to 4, and complains that it cannot apply a this Boolean operator to an integer. If-Then-Else Operator if-then-else is used like?: in C. Types It is an expression-operator, not a statement as is \if-else" is in C. if 3<4 then 5 else 6; val it = 5 : int Four basic types: int, real, bool, string. Values are denoted as in C, but bool has only values true and false. real in ML is float in C. string is a basic type in ML, not an array of characters as in C. Types Must Agree ML will gure out the type for most expressions, using clues such as the types of arguments. But there is no automatic coersion, as from int to oat in C. Example: 3 + 4.0; std in:2.1-2.7 Error: operator and operand don't agree (tycon mismatch) operator domain: int * int operand: int * real in expression: + : overloaded (3,4.0) 4

ML views every operator as applying to a single operand. Even a binary, inx operator is thought of as applying to a pair, e.g. the pair (3; 4:0) of type int * real. Many ML operators like * are overloaded; they can apply to operands of various types, in the case of * to either a pair of integers, a pair of reals, or a pair of types. Notice that * in addition to its arithmetic role also is used to build structure-types, such as pair-types in this example. When ML sees the 3 and then the *, it assumes that the int * int version of * is meant. ML complains when its operand turns out to be of type int * real. I think that the line numbers in SML/NJ error messages are too high by 1. \std in:2.1-2.7" is supposed to mean that the error occurs in characters 1{7 of line 2, but in this example, there was only one line of input. Coercion There are a number of operators that convert from one type to an \equivalent" value in another type. See pp. 17{18 and 249{250 of EMLP. Example: 3.14159 * real(2); val it = 6.28318 : real floor(3.14159); val it = 3 : int ord("#"); val it = 35 : int chr(35); val it = "#" : string ML Identiers Names of variables in ML may be formed in one of two ways: 5

1. Alphanumeric identiers, like identiers in C, but the apostrophe ' may also be used as a letter. However, an identier beginning with ' may only have a type as a value, not an \ordinary" value." 2. Symbolic identiers are strings composed of 20 dierent symbols, mostly the usual operator symbols (see p. 20 of EMLP for complete list). Tuples Thus, ordinary operator names like * or <= are symbolic identiers. So would *~, which explains why 3*~4 is not interpreted \correctly." A tuple is a parenthesized list of values of any type. Tuples are like structs in C, but without component names (but ML also has the ability to name components as we shall see much later). (4, 4.0, "four"); val it = (4,4.0,"four") : int * real * string Note that the type of a tuple is the list of the types of its components separated by *'s. We extract a component of a tuple with the #i operator; i is any integer for which there is a component. #2(4, 4.0, "four"); val it = 4.0 : real Lists A list is a sequence of values surrounded by square brackets and separated by commas. Unlike tuples, which use round rather than square brackets, the elements of a list must have the same type. 6

["a", "b", "c"]; val it = ["a","b","c"] : string list [(1,2), (3,4)]; val it = [(1,2),(3,4)] : (int * int) list The type of the rst list is string list, i.e., a list of strings. The second list is a list of pairs of integers. Note: The empty list is denoted by [] or nil. Operators on Lists hd and tl extract the head (rst element) and tail (list of the remaining elements). hd([1,2]); val it = 1 : int tl([1,2]); val it = [2] : int list tl [1]; val it = [] : int list Note the type of the head is the type of an element, while the type of the tail is a list of elements. Notice in the last example that parentheses are not needed for arguments of one-argument functions in ML. :: is the cons operator; it connects a head and a tail to form a new list. 1::[2,3]; val it = [1,2,3] : int list @ is the concatenation operator for lists (not for strings, where ^ is used). [1]@[2,3]; val it = [1,2,3] : int list 7