Docsity

Programming in ML: CS 3721 Lab #05, Lab Reports of Programming Languages

Toggle sidebar
bg1
CS 3721: Programming Languages Lab
Lab #05: Programming in ML
ML is a statically typed functional programming language. In particular, ML is both
compiled and interpreted. Similar to Scheme, it supports functional programming; similar
to C/Pascal, it also supports statements and a wide variety of compound data structures
including lists, tuples, records(similar to C struct), and user-defined data types.
If you have a ML program, say myprogram.ml, you can compile and run your program by
typing
>sml <myprogram.ml
The redirection ensures that the ML environment reads your program, processes all the
statements and expressions, and then exits. NOTE: All ML statements must termi-
nate with semicolons (‘;’). ML will not start interpreting your expression unless
you place a ‘;’ at the end (which makes it a statement).
Finish the following exercises to get you familiar with ML syntax and concepts. Open a text
file with ”.sml” as extension and save all your solutions. In particular, use a comment above
each solution to indicate the corresponding exercise number. Each ML comment must be
enclosed within a pair of (∗and ∗).
(* This is a comment. sml will not attempt to execute any part of it. *)
After you are done, submit your sml file
www.cs.utsa.edu/~cs3723
1. ML supports five types of atomic values: unit, bool, int, real, and string. Give an
example for each type of values (when possible, different from the ones shown below).
(); (5+3)-2; 3.0 + 4.2; 5 = 4; "abc";
2. ML provides three operations to support bool values: andalso,orelse, and not. Given
an example that uses each operator (different from the ones shown below).
3 = 3 andalso 2 = 2; not (3 = 2 orelse 2 = 2);
3. ML provides five operations to support int values: +,−,∗, div, mod; The operations
for supporting real values are +,−,∗, /. Note that /can be used for dividing floating
point values only. Give an integer and a real expression that use the above operators
respectively (different from the ones shown below). What happens when you mix
integer and real values?
2+3*5-2div1+5mod2; 2.15*5.0-2.1/3.1+3.5;
4. The ∧operator is provided to support string concatenation. Given an example for
the operator (different from the ones shown below).
"abc" ^ "def"; ==> val it = "abcdef" : string
5. ML supports the following types of compound values: list, tuple, record, function, and
reference (similar to C pointers). Give an example for each type of values (different
from the ones shown below).
1
pf2

Partial preview of the text

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

CS 3721: Programming Languages Lab Lab #05: Programming in ML

ML is a statically typed functional programming language. In particular, ML is both compiled and interpreted. Similar to Scheme, it supports functional programming; similar to C/Pascal, it also supports statements and a wide variety of compound data structures including lists, tuples, records(similar to C struct), and user-defined data types.

If you have a ML program, say myprogram.ml, you can compile and run your program by typing

sml < myprogram.ml The redirection ensures that the ML environment reads your program, processes all the statements and expressions, and then exits. NOTE: All ML statements must termi- nate with semicolons (‘;’). ML will not start interpreting your expression unless you place a ‘;’ at the end (which makes it a statement).

Finish the following exercises to get you familiar with ML syntax and concepts. Open a text file with ”.sml” as extension and save all your solutions. In particular, use a comment above each solution to indicate the corresponding exercise number. Each ML comment must be enclosed within a pair of (∗ and ∗).

(* This is a comment. sml will not attempt to execute any part of it. *)

After you are done, submit your sml file

www.cs.utsa.edu/~cs

  1. ML supports five types of atomic values: unit, bool, int, real, and string. Give an example for each type of values (when possible, different from the ones shown below). (); (5+3)-2; 3.0 + 4.2; 5 = 4; "abc";
  2. ML provides three operations to support bool values: andalso, orelse, and not. Given an example that uses each operator (different from the ones shown below). 3 = 3 andalso 2 = 2; not (3 = 2 orelse 2 = 2);
  3. ML provides five operations to support int values: +, −, ∗, div, mod; The operations for supporting real values are +, −, ∗, /. Note that / can be used for dividing floating point values only. Give an integer and a real expression that use the above operators respectively (different from the ones shown below). What happens when you mix integer and real values? 2 + 3 * 5 - 2 div 1 + 5 mod 2; 2.15 * 5.0 - 2.1 / 3.1 + 3.5;
  4. The ∧ operator is provided to support string concatenation. Given an example for the operator (different from the ones shown below). "abc" ^ "def"; ==> val it = "abcdef" : string
  5. ML supports the following types of compound values: list, tuple, record, function, and reference (similar to C pointers). Give an example for each type of values (different from the ones shown below).

1::[2,3,4]; (3,4); {f1 = "a", f2="b"}; fn x => x + 5; ref 3;

  1. What operations can be used to build a list, test whether a list is empty, and pull apart values in the list? Give an example for each operation (different from the examples given below). Examples: null []; hd (2 :: [3, 4, 5]); tl(2 :: [3,4,5]);
  2. What operations can be used to build a tuple and to pull apart values in a tuple? Give an example for each operation (different from the examples given below). (3,4); #1(3,4); #2(3,4);
  3. What operations can be used to build a record and to pull apart values in a record? Give an example for each operation (different from the examples given below). {f1 = "a", f2="b"}; #f1{f1 = "a", f2="b"}; #f2{f1 = "a", f2="b"};
  4. What operations can be used to build a function and to invoke a function with ar- guments? Give an example for each operation (different from the examples given below). fn x => x + 5; (fn x => x + 5) 6;
  5. What operations can be used to build a reference cell (similar to a C pointer), to modify a reference cell, and to extract the value of a reference cell? Give an example for each operation (different from the examples given below). val x = ref 3; x := 5; !x;
  6. How to define new variables in ML? (val, fun) Give two examples different from the examples given below. val t = (1, 2, 3); val (x,y,z) = t; fun plus(x,y) = x + y;
  7. How to define new data types in ML? Give an example of a user-defined union type (different from the one below) and give a value of your user-defined type. datatype MyType = TypeInt of int | TypeFloat of real | Unit; TypeFloat(2.3);
  8. What control-flow operations are supported in ML? (if-then-else, while) Give an ex- ample for each operation together with the result of evaluation (different from the ones show below). if true then 1 else 5; val x = ref 0; while (!x < 5) do x := !x + 1;
  9. Write a function genN um which takes a single parameter n and returns a ML list that contains all integer numbers from 1 through n.