Sml

Cards (17)

  • Standard ML (SML)
    A newer version of the "Meta Language" or ML programming language developed in the 1970s
  • ML
    • A functional programming language
    • Functions are first-class data objects: they may be passed as arguments, returned as results, and stored in variables
    • The principal control mechanism in ML is recursive function application
  • ML
    • An interactive language: Every phrase read is analyzed, compiled, and executed, and the value of the phrase is reported, together with its type
    • Strongly typed: Every legal expression has a type which is determined automatically by the compiler
    • Strong typing guarantees that no program can incur a type error at run time
  • ML
    • Has a polymorphic type system: Each legal phrase has a uniquely determined most general typing that determines the set of contexts in which that phrase may be legally used
    • Statically scoped: ML resolves identifier references at compile time, leading to more modular and more efficient programs
  • ML
    • Supports abstract types: Abstract types are a useful mechanism for program modularization. New types together with a set of functions on objects of that type may be defined. The details of the implementation are hidden from the user of the type, achieving a degree of isolation that is crucial to program maintenance
  • ML
    • Has a type-safe exception mechanism: Exceptions are a useful means of handling unusual or deviant conditions arising at run-time
    • Has a modules facility to support the incremental construction of large programs: An ML program is constructed as an interdependent collection of structures which are glued together using functors. Separate compilation is supported through the ability to export and import functors
  • Hello, world in SML

    • Standard ML of New Jersey, - print("Hello world\n");
    • Hello world
    • val it = () : unit
  • Arithmetic in ML
    • 2+2; (* note semicolon at end*)
    • 4/3; (* an error! *)
    • 6 div 2; (* integer division *)
  • Arithmetic in ML

    • ML is picky about not mixing types, such as int and real, in expressions
    • The value of "it" is always the last value computed
    • Function arguments don't always need parentheses, but it doesn't hurt to use them
  • Types of arguments and results
    • ML figures out the input and/or output types for simple expressions, constant declarations, and function declarations
    • If the default isn't what you want, you can specify the input and output types, e.g. fun divBy2 x:int = x div 2 : int;
    • fun divideBy2 (y : real) = y / 2.0;
  • Strings
    • Delimited by double quotes
    • The caret mark ^ is used for string concatenation, e.g. "house"^"cat"
    • \n is used for newline, as in C and C++
  • Making Lists
    • The @ operator is used to concatenate two lists of the same type
    • The functions hd and tl give the first element of the list, and the rest of the list, respectively
  • Strings and Lists
    • The explode function converts a string into a list of characters
    • The implode function converts a list of characters into a string
  • HEAD AND TAILS
    The cons operator :: takes an element and prepends it to a list of that same type.
  • Strongly typed
    A programming language in which every legal expression has a type, and this type is determined automatically by the compiler
  • Polymorphic type system

    A type system that allows a single expression to have multiple types, determining the set of contexts in which that phrase may be legally used
  • Statically scoped
    A language in which identifier references are resolved at compile time, rather than at runtime, leading to more modular and more efficient programs