Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Standard ML Programming

25% developed
From Wikibooks, open books for an open world
Thelatest reviewed version waschecked on8 May 2022. There aretemplate/file changes awaiting review.

Contents

[edit |edit source]
Aprintable version of Standard ML Programming is available. (edit it)

Since this book is far from completion, existing pages are listed here.

  1. Types
  2. Expressions
  3. Examples and Exercises
  4. Solutions

About Standard ML

[edit |edit source]

Standard ML (SML) belongs to the ML family of programming languages. Like other members of this family (such as OCaml), it is characterized by strong static typing with type inference, strict evaluation, and a module system which features signatures, structures and functors. The current standard dates from 1997; it supersedes an earlier standard from 1990, and the following standard is known as successor ML (sML).

Syntax examples

[edit |edit source]

The following code snippet defines a functionfactorial that computes the factorial of a non-negative integern.

localvalrecfac=fn0=>1|n=>n*fac(n-1)infunfactorialn=ifn<0thenraiseDomainelsefacnend;

One important note: in the interactive top-level of SML/NJ, the terminal semicolon is necessary to evaluate the code so far, but the semicolon is usually optional as a statement terminator; it is only required to segregate sequenced expressions.

The following code snippet defines a functiongray_code that maps an integern to a list of all distinctn-character strings of 0s and 1s, inGray code order, such that each element differs from its neighbour(s) in exactly one character, before computing a string of three-digit gray codes which is equal to"000-001-011-010-110-111-101-100".

funprefixps=String.concat[p,s];funextendpxs=List.map(prefixp)xs;funexpandprev=extend"0"prev@List.rev(extend"1"prev);fungray_coden=ifn<1then[""]elseexpand(gray_code(n-1));funprint_linea=printabeforeprint"\n";print_line(String.concatWith"-"(gray_code3));

Getting started

[edit |edit source]

A prerequisite for starting with Standard ML is the compiler, most of which generate machine code directly; however, like most dynamic languages, several SML compilers offer an interactive top-level which compiles and evaluates code on demand, which can be convenient for froshes and underclassmen.

External resources

[edit |edit source]
About Standard ML
Compilers and interpreters
  • MLton: A whole-program optimizing compiler without an interactive top-level
  • Standard ML of New Jersey: A compiler with an interactive top-level and libraries
  • Alice ML: A multi-platform interpreter with GUI
Retrieved from "https://en.wikibooks.org/w/index.php?title=Standard_ML_Programming&oldid=4013088"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp