HMM Presentation

advertisement
CS345 Project Presentation
Language: H-Mikhail Iakhiaev
Features Overview
•
•
•
•
•
•
List Comprehension
Support for Tuples and Lists
Lambda expressions
For-each loop
Support for Strings
Miscellaneous features
– Declarations can appear anywhere
– Built-in functions: "print", "println".
Typing
• Basic Types
– int, bool, float, void, string, list, tuple, object.
• Function types:
– "(bool, bool, bool)": like Huskell
• Mixed static/dynamic typing:
int a = 2, b = 3, c;
c = a + b; // Integer plus
float x = 5.0, y;
y = b + x - a; // Implicit int->float
int a = 2;
object c;
list b = getValues();
c = a + b[2]; // Object plus
Lists/Tuples
• Support for immediate values:
– list a = [1, 2, 3]; tuple b = ("John", 4);
• Arbitrary level of nesting
• Accessing the elements: a[3].
• Difference: tuple elements can not be assigned
values.
list createDept()
{
return [ (10, "ACCOUNTING", "NEW YORK"),
(20, "RESEARCH", "DALLAS"),
(30, "SALES", "CHICAGO"),
(40, "OPERATIONS", "BOSTON") ];
}
List Comprehension
list emp = createEmp();
list dept = createDept();
list dbRes = [ (name, date, city) |
(_, name, _, _, date, _, d1) <- emp,
(d2, _, city) <- dept,
d1 == d2 ];
println("Query Result: ", dbRes);
•
•
•
•
Multiple "generators" and conditions
"_" are properly recognized
Easy to implement the joins of different lists.
List comprehension actually declares variables "name",
"city", and "date" (all object).
List Comprehension(2)
[ (name, date, city) | (_, name, _, _, date, _, d1) <- emp, (d2, _, city) <- dept, d1 == d2];
C:\Documents and
Settings\miakhiae\Desktop\proj>hmm test.c
Query Result: [
(KING, 17-NOV-81, NEW YORK),
(BLAKE, 01-MAY-81, CHICAGO),
(CLARK, 09-JUN-81, NEW YORK),
(JONES, 02-APR-81, DALLAS),
(SCOTT, 09-DEC-82, DALLAS),
(FORD, 03-DEC-81, DALLAS),
(SMITH, 17-DEC-80, DALLAS),
(ALLEN, 20-FEB-81, CHICAGO),
(WARD, 22-FEB-81, CHICAGO),
(MARTIN, 28-SEP-81, CHICAGO),
(TURNER, 08-SEP-81, CHICAGO),
(ADAMS, 12-JAN-83, DALLAS),
(JAMES, 03-DEC-81, CHICAGO),
(MILLER, 23-JAN-82, NEW YORK)
]
Lambda Expressions
int main () {
bool eqRes = equivalence2( (\ x, y -> !(x || y)),
println("Result: ", eqRes);
}
(\ x, y -> !x && !y) );
bool equivalence2( (object, object, object) a, (object, object, object) b )
{
list l = [a(x, y) == b(x, y) | x <- [true, false], y <- [true, false]];
return and(l);
}
• Use Function Types: (arg1, ... argN, returnType)
• "First-class citizens", because they can be passed into
the other functions just like other variables.
• Only variables of the Function Types can be executed as
Lambdas
• Note, the "x, y" variables are declared by each Lambda
For-each loop
• Somewhat similar to List Comprehension
• Executes the body instead of creating a list.
• Declares the loop variable.
bool and(list lst)
{
bool result = true;
for (l <- lst)
{
result = result && l;
}
return result;
}
object calcExpense(list emp)
{
object sum = 0;
for ((_, _, _, _, _, sal, _) <- emp) {
sum = sum + sal;
}
return sum;
}
Scoping/Binding
• Static Scoping. The scopes can be:
– Block
– List comprehension
– Lambda Definition
• Static Binding
– Variables in the Lambda expression are bound to
where the Lambda is defined.
• Function arguments:
– List/tuple: pass by reference.
– Other: pass by value
Static Binding example
int main() {
list emp = createEmp();
int x = 6000;
println( selectDept20(emp, getSelector()) );
}
(object, bool) getSelector() {
int x = 1000;
return (\ y -> y < x);
}
OUTPUT:
[
(SMITH, 800)
]
list selectDept20(list emp, (object, bool) selector) {
int x = 20;
return [ (name, sal) | (_, name, _, _, _, sal, dept) <- emp,
selector(sal), dept == x];
}
• Note, the variable x in the Lambda expression binds to the x in the
getSelector, not the one in the main or selectDept20.
Strings are Fun
println("All the salesmen: ",
[ (name, sal) |
(_, name, pos, _, _, sal, _) <- createEmp(),
pos == "SALESMAN" ]);
println("People who alphabetically follow Martin: ",
[ name | (_, name, _, _, _, _, _) <- createEmp(), name > "MARTIN"]);
C:\Documents and Settings\miakhiae\Desktop\proj>hmm test_str.c
All the salesmen: [
(ALLEN, 1600),
(WARD, 1250),
(MARTIN, 1250),
(TURNER, 1500)
]
People who alphabetically follow Martin: [SCOTT, SMITH, WARD, TURNER, MILLER]
Questions?
• ????
Download