ML Programming Lab: Understanding ML Expressions and Operations, Lab Reports of Programming Languages

A lab guide for cs 3721 students to learn how to interact with ml, write simple expressions using primitive types (int, real, bool, string), and perform operations on them. It covers arithmetic, boolean, and string operations, as well as tuples and value declarations.

Typology: Lab Reports

Pre 2010

Uploaded on 07/30/2009

koofers-user-4gy
koofers-user-4gy 🇺🇸

7 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 3721: Programming Languages Lab
Lab #07: Programming in ML
Due date: March 11, 3:30pm. At the beginning of the next recitation.
Goals of this lab:
Be able to interact with ML and interpret ML’s response ;Be able to write simple
expressions of numbers, booleans and tuples ;Be able to list several of ML’s basic
primitive types and write expressions belonging to those types ;Be able to read/write
expressions constructing tuples ;Be able to write val declarations and expressions using
pattern matching on tuples ;Be able to declare functions using fun
1. Primitive types. For ML expressions, user interaction with the ML compiler has the
form:
-<expression>;
val it = <value> : <type>
First line is the user input, and the second one is output from the ML compiler and run-
time system. it is a special identifier bound to the value of the last expression entered.
Therefore, “it = <value>:<type> means that the value of the expression is < value >
and its type is < type >. The keyword val stands for value. Now, you’ve learned how
to interpret the output from the ML compiler. Followings are some expressions. Please,
write down their values and types (fill in the blanks).
- 3487 + (45 - 691); ==> val _____ = _____ : _____
- "cs utsa"; ==> val _____ = _____ : _____
- 55 <> 4; ==> val _____ = _____ : _____
- (14.3 - 3.7) + 5.8; ==> val _____ = _____ : _____
- it + 3.6; ==> val _____ = _____ : _____
2. Operations provided to support int values. “+, -, *, div are binary infix operators on
integers. Find out the result of 2+361
3. Write down your ML expression, and the
output. (In ML, 1-tuples are used for grouping subexpressions like parentheses used in
C/JAVA.)
3. Operations provided to support real values. “+, -, *, /” are binary infix operators on
reals. Find out the result of 2.4 + 3.26.21 1.1
3.4. Write down your ML expression, and the
output.
4. Operations provided to support bool values. “andalso, orelse, not” are operators on bools.
Find out the result of the expression, “(true 2 = 2) 5<> 5 ¬(3 = 4)”.
Write down your ML expression, and the output. (“<> is the not equal operator.)
1
pf3

Partial preview of the text

Download ML Programming Lab: Understanding ML Expressions and Operations and more Lab Reports Programming Languages in PDF only on Docsity!

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

Due date: March 11, 3:30pm. At the beginning of the next recitation.

Goals of this lab:

  • Be able to interact with ML and interpret ML’s response ; Be able to write simple expressions of numbers, booleans and tuples ; Be able to list several of ML’s basic primitive types and write expressions belonging to those types ; Be able to read/write expressions constructing tuples ; Be able to write val declarations and expressions using pattern matching on tuples ; Be able to declare functions using fun
  1. Primitive types. For ML expressions, user interaction with the ML compiler has the form: -; val it = : First line is the user input, and the second one is output from the ML compiler and run- time system. it is a special identifier bound to the value of the last expression entered. Therefore, “it = : ” means that the value of the expression is < value > and its type is < type >. The keyword val stands for value. Now, you’ve learned how to interpret the output from the ML compiler. Followings are some expressions. Please, write down their values and types (fill in the blanks).
  • 3487 + (45 - 691); ==> val _____ = _____ : _____
  • "cs utsa"; ==> val _____ = _____ : _____
  • 55 <> 4; ==> val _____ = _____ : _____
  • (14.3 - 3.7) + 5.8; ==> val _____ = _____ : _____
  • it + 3.6; ==> val _____ = _____ : _____
  1. Operations provided to support int values. “+, -, *, div” are binary infix operators on integers. Find out the result of 2 + 3^ ∗ 3 6 −^1. Write down your ML expression, and the output. (In ML, 1-tuples are used for grouping subexpressions like parentheses used in C/JAVA.)
  2. Operations provided to support real values. “+, -, *, /” are binary infix operators on reals. Find out the result of 2 .4 + 3.^2 3 ∗.^46.^21 −^1.^1. Write down your ML expression, and the output.
  3. Operations provided to support bool values. “andalso, orelse, not” are operators on bools. Find out the result of the expression, “(true ∧ 2 = 2) ∨ 5 <> 5 ∨ ¬(3 = 4)”. Write down your ML expression, and the output. (“<>” is the not equal operator.)
  1. Operation provided to support string values. String concatenation operator, “ˆ”, is pro- vided for string values. For example,
    • "Baris" ^ " " ^ "Tas"; ==> val it = "Baris Tas" : string Please, write down an expression that concatenates your full name (First, last, and middle names - if you have a middle name-.), and the output.
  2. Erroneous operations. As we have seen, the arithmetic operators “+, -, *” may be ap- plied to either integers or real numbers. Note that when + has two integer arguments the result is an integer, and when + has two real arguments the result is a real number. However, it is a type error to combine integer and real arguments. Followings are erro- neous expressions. For each expression, what causes the error? (Hint: Error messages tell a lot!)
    • 5 - 3.2; ==> reason:
    • not 5 = 5; ==> reason:
    • 6 andalso true; ==> reason:
    • 5 / 2; ==> reason:
    • 7.3 div 5.2; ==> reason:
    • 4 ^ 4; ==> reason:
    • if true then 3 else false; ==> reason:
  3. Tuples. A tuple may be a pair, triple, quadruple, and so on. They may be formed of any types of values. Followings are some expressions containing tuples. Please, write down their values and types as you did in question 1.
    • (4, 5); ==>
    • (3.2, 4.3, 3.1); ==>
    • (1, 2.2, 3, 3.3); ==>
    • (4+3, 2.1+3.1, 2=2, 2<>2 orelse false, "utsa"); ==> The operator “#” is provided to pull apart the components in a tuple. For example, # selects the first component, #2 selects the second component, and so on. An example:
    • #2(1, 2, 3); ==> val it = 2 : int (* 2nd component is accessed.*) Now, write an expression in order to access the fourth component of the following tuple: (4+3, 2.1+3.1, 2=2, 2<>2 orelse false, "utsa"). Also, write down the output.
  4. Value declarations. A pattern is an expression containing variables such as x, y, z, ... and constants combined by certain forms such as tupling. And the general form of value declaration associates a value with a pattern. This form is: val = ;