Functional Programming Using F# by Hansen Michael R. & Rischel Hans

Functional Programming Using F# by Hansen Michael R. & Rischel Hans

Author:Hansen, Michael R. & Rischel, Hans [Hansen, Michael R.]
Language: eng
Format: epub
Publisher: Cambridge University Press
Published: 2013-04-29T22:00:00+00:00


8.4 Sequential composition

The semicolon symbol “;” denotes the sequential composition operator (while the double semicolon “;;” is a terminator symbol). This operator combines two expressions exp1 and exp2 to form a new expression:

exp1; exp2

The expression exp1 ; exp2 is evaluated as follows:

Evaluate exp1 and discard the result.

Evaluate exp2 and supply the result as the result of evaluating exp1; exp2.

Hence, if exp2 has type τ then exp1 ; exp2 has type τ as well.

The F# compiler issues a warning if exp1 is of type different from unit as the result of the evaluation might be of some use. This warning is avoided by using the ignore function:

ignore(exp1) ; exp2 or exp1|> ignore ; exp2

where ignore a = () for any value a.

We may combine two assignments using sequential composition:

let mutable x = 5;; let mutable y = 7;; x <- y + 1 ; y <- x + 2;; (x,y);; val it : int * int = (8 ,10)

Note that the second assignment uses the new value stored in the location denoted by x.

The operator “;” may be omitted if the expressions are written on separate lines, that is,

exp1

exp2

means (exp1 ) ; exp2.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.