CS 3721 Lab Solutions: Imperative Programming in ML, Lab Reports of Programming Languages

Solutions to lab #09 of cs 3721: programming languages, focusing on imperative programming in ml using reference cells and loops. It includes instructions for creating reference cells, examples of reference cell usage, and the recursive and imperative versions of functions sum and gcd.

Typology: Lab Reports

Pre 2010

Uploaded on 07/30/2009

koofers-user-xsb
koofers-user-xsb šŸ‡ŗšŸ‡ø

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 3721: Programming Languages Lab
Lab #09 Solutions: Imperative Programming in ML
Due date: April 1, 3:30pm. At the beginning of the next recitation.
Goals of this lab:
•Be able to read/write short imperative programs in ML using ref cells, if, while do.
1. Reference Cells. Areference cell is the assignable region of memory in ML. A reference is itself
a value; if xhas type τthen a reference to xis written ref x and has type τref. Operations on
reference cells are:
re f v => c rea te s a re f er enc e ce ll co nta in ig va lue v
!r => d ere fe r en cin g : re tur ns t he va lu e c on tai ne d i n reference cell r
r := v = > pl ace s val ue v i n reference cell r
Reference cells correspond to the variables of C, Pascal and similar languages where as the value
declarations that we have seen so far correspond to the constants of languages like C. Here are
some examples:
-val r = re f 5;
va l r = ref 5 : int ref
The identifier ris bound to a new reference cell with contents 5. The output says that the value
of ris this reference cell having type int ref.
- r := !r + 1;
va l it = () : un it
The value in the reference cell, r, is incremented.
- !r ;
va l it = 6 : i nt
!r returns the contents of r, which is the integer 6.
(a) Create three real reference cells x,yand zwhich have the initial values 3.5, 4.2 and 5.1,
respectively. Then, assign the mathematical expression, x+yāˆ—xāˆ’y
zto the variable x(in this
case, the reference cell x.). Finally, return the contents of x. (Use let.)
Solution:
le t
val x = re f 3 .5 ;
val y = re f 4 .2 ;
val z = re f 5 .1
in
x := ( ! x + !y * ! x - ! y) / ! z ;
!x
en d ;
=> val it = 2.74509803922 : real
1
pf3

Partial preview of the text

Download CS 3721 Lab Solutions: Imperative Programming in ML and more Lab Reports Programming Languages in PDF only on Docsity!

CS 3721: Programming Languages Lab Lab #09 Solutions: Imperative Programming in ML

Due date: April 1, 3:30pm. At the beginning of the next recitation.

Goals of this lab:

  • Be able to read/write short imperative programs in ML using ref cells, if, while do.
  1. Reference Cells. A reference cell is the assignable region of memory in ML. A reference is itself a value; if x has type Ļ„ then a reference to x is written ref x and has type Ļ„ ref. Operations on reference cells are:

ref v => creates a reference cell containig value v !r => dereferencing : returns the value contained in reference cell r r := v => places value v in reference cell r Reference cells correspond to the variables of C, Pascal and similar languages where as the value declarations that we have seen so far correspond to the constants of languages like C. Here are some examples:

  • val r = ref 5; val r = ref 5 : int ref The identifier r is bound to a new reference cell with contents 5. The output says that the value of r is this reference cell having type int ref.
  • r := !r + 1; val it = () : unit The value in the reference cell, r, is incremented.
  • !r; val it = 6 : int !r returns the contents of r, which is the integer 6. (a) Create three real reference cells x, y and z which have the initial values 3.5, 4.2 and 5.1, respectively. Then, assign the mathematical expression, x^ +^ y^ āˆ—z x^ āˆ’^ yto the variable x (in this case, the reference cell x.). Finally, return the contents of x. (Use let.) Solution:

let val x = ref 3.5; val y = ref 4.2; val z = ref 5. in x := (!x + !y * !x - !y) / !z; !x end;

=> val it = 2.74509803922 : real

(b) Define a reference cell nameList which initially has empty list as its value. Then, add your first and last name to this list using the cons operator ā€œ::ā€. The final value of the reference cell should be [ā€œfirstNameā€, ā€œlastNameā€]. Return the value of the reference cell.

Solution:

val nameList = ref ([]:( string list )); nameList := "baris" :: "tas" ::! nameList ; ! nameList ;

  1. Loops. For iteration, ML has a while command, and its syntax is: while E 1 do E 2 If more than one expression is needed in the while body, the following syntax is used: while E 1 do (E 2 ; E 3 ; ...; En) Example:

val i = ref 0; while !i < 10 do i := !i + 1;

Now, write both recursive and imperative versions of the function sum that computes the expres- sion,

āˆ‘^ n

i= 1

i. The function takes one argument which is n in the summation. (Use let, while, and

reference cells in the imperative version.) Test your functions with the following:

  • sum (15); val it = 120 : int

(a) Purely functional version: Solution:

fun sum(n) = if n = 1 then 1 else n + sum(n -1);

(b) Imperative version: Solution:

fun sum(n) = let val i = ref 1; val total = ref 0; in while !i<=n do ( total := !total + !i; i := !i + 1 ); !total end;