A Functional Programming Language: ML

A Programming Language is:

  1. a language for a virtual compuation environment
  2. a very formal language
  3. precise syntax (structure)
    1. Which symbols can be used, and how can they be composed
  4. precise semantics (meaning)
    1. What do the symbols mean

ML is:

  1. a functional programming language (i.e., like math)
  2. can be interactive, or programmatic

ML Expressions -- Composition of values and operators, ended with a semicolon

ML evaluates the expression given, and prints the result

  1. the result has a name - it
  2. the result has a value - 7
  3. the result has a type - int

Type is a very important and fundamental concept

  1. defines a set of values that can be used
  2. expressions must have type consistency
  3. so, integers, reals, booleans, and strings are the fundamental types in ML


ML has Constants of four types:

  1. Integers -- a string of digits (0-9), optionally preceded by a tilde (~) to indicate negative
    1. Do we really need to say what an integer is? Yes.
    2. Can it have 0s in front? What about writing out, like a check?
  2. Reals -- string of digits (possibly preceded by ~), followed by at least one of
    1. a decimal point and one or more digits
    2. the letter E, followed by an integer
  3. Booleans -- true or false
  4. Strings -- a sequence of characters enclosed with double-quotes (")
    1. \ is a special character, and means that the next character (or more) indicate something special
    2. \n is the newline character, basically just like hitting the Return key
    3. \t is the tab character -- like hitting the Tab key
    4. \\ is the backslash -- since one \ is special, just type \\ to really mean backslash
    5. \" is the double-quote -- a plain double-quote would mean the end of the string, to have a double-quote actually be in the string, you use \"
    6. \### is a way to specify a character by its ASCII number
    7. \^A for any letter A is a way to specify a control character
    8. \ as the last character on the line means the string continues on the following line


Arithmetic operators: +,-,*,/,div,mod,~

Question: What order of evaluation?

Answer: operators have precedence relations

  1. negation (unary minus) is evaluated first (highest precendence)
  2. multiplication operators are next
  3. addition operators are last
  4. parentheses, of course, override these rules (technically, they have highest precedence)

Question: When are spaces needed?

Answer: when ML can't figure it out itself

String operators: 

  1. ^ is concatenation; that is, it makes one string out of two by slapping them together, end to end
  2. "" is an empty string, and is allowed

Comparison (Relational) Operators: =, <, >, <=, >=, <>

  1. These can compare two values of the same type: integer, real, or string
  2. The result is a value of type Boolean (true or false)
  3. They have lower precedence than any arithmetic operators (i.e., evaluate after)

Logical Operators: andalso, orelse, not

  1. They can only operate on Boolean values, and result in Boolean values
  2. Even lower precedence than Comparison operators, and orelse is lower than andalso
  3. orelse is an inclusive or, as opposed to an exclusive or
  4. Truthtables of each (done in class)

If-then-else Operator

  1. An operator? Yes, it takes operands and evaluates to a result
  2. The form is if expr1 then expr2 else expr3
  3. expr1 must result in a Boolean value
  4. expr2 and expr3 must result in values of the same type
  5. the result of the if-then-else operator is the value of expr2 if expr1 is true, otherwise it is the value of expr3; the type of the result is the type of expr2 and expr3
  6. Think about disjoint functions in math

Examples on slides are found here