a > b
is 1 if a is greater than b, 0
otherwise.
v := e
changes the value of
the variable v
in the current environment.
while (e) s
evaluates the guard
expression e
. If the guard is 0, nothing else
happens. If the guard is not 0, the body statement s
is executed and the whole action repeats.
<exp> ::= fn <var> => <exp> | <addexp> <addexp> ::= <addexp> + <mulexp> | <mulexp> <mulexp> ::= <mulexp> * <funexp> | <funexp> <funexp> ::= <funexp> <rootexp> | <rootexp> <rootexp> ::= let val <var> = <exp> in <exp> end | ( <exp> ) | <var> | <const>Here is a sample program in Language Four, showing all the new constructs:
let val fact = fn x => if x < 2 then x else x * fact(x-1) in fact 5 endAs you can see, Language Four extends Language Three with three new constructs: the
<
operator for comparison, the
-
operator for subtraction, and the conditional
(if-then-else
) expression. The sample program above
defines a recursive factorial function and uses it to compute the
factorial of 5. Define the syntax of Languague Four by extending
the syntax of Language Three and give a natural semantics for
Language Four.
f
produced by fun f ...
includes the function body being
defined, but the scope of the definition of f
produced
by val f = fn...
does not. So in ML, only the
fun
definitions can be recursive. Extend the syntax of
Language Four to allow simple definitions with fun
, so
that, for example, this program is legal:
let fun f x = x + 1 in f 1 endAlso, give a natual semantics for your extended language.
Quicksort(L, X)
predicate in Prolog where
L
is a list of unsorted integers and X
is
the sorted listed from L
.