Syntax errors in main function - SML/NJ [deleting DO VAL, deleting VAL ID, SEMICOLON ID, deleting SEMICOLON END SEMICOLON] - sml

May someone highlight to me why I am getting the syntax errors for the main function, so that I can fix it. I am quite new to the language. Actually I was introduced to it through the assignment, so I am totally lost as to how to refactor it to avoid the syntax error:
val IDs = [410021001,410021002,410021003,410021004,410021005,410021006,410021007,410021008,410021009,410021010];
val Names = ["Alan","Bob","Carrie","David","Ethan","Frank","Gary","Helen","Igor","Jeff"]: string list;
val HW1 = [90.0,85.0,90.0,117.0,85.0,90.0,117.0,117.0,117.0,117.0] : real list;
val HW2 = [84.5,49.0,110.5,85.0,56.0,65.0,65.0,59.5,50.0,50.0] : real list;
val HW3 = [117.0,117.0,117.0,0.0,65.0,117.0,50.0,51.0,75.0,75.0] : real list;
val Midterm = [60.0,57.0,6.0,44.0,72.0,43.0,54.0,75.0,53.0,75.0] : real list;
val Final = [66.0,64.0,62.0,55.0,66.0,75.0,75.0,75.0,75.0,75.0] : real list;
fun score(HW1, HW2, HW3, Midterm, Final) =
round(HW1 * 0.1 + HW2 * 0.1 + HW3 * 0.1 + Midterm * 0.3 + Final * 0.4);
fun letterGrade(score) =
if score >= 90 then "A+"
else if score >= 85 then "A"
else if score >= 80 then "A-"
else if score >= 77 then "B+"
else if score >= 73 then "B"
else if score >= 70 then "B-"
else if score >= 67 then "C+"
else if score >= 63 then "C"
else if score >= 60 then "C-"
else if score >= 50 then "D"
else "E";
val i = 0
val max = length(IDs)
fun main() =
while i < max do
var ind_score = score(HW1[i], HW2[i], HW3[i], Midterm[i], Final[i])
var grade = letterGrade(ind_score)
print(IDs[i], " ", Names[i], " ", ind_score, " ", grade)
i = i + 1
end
end
This is the error I am producing after running my programme, which shows that my errors start at this function:
Terminal feedback

Part 1 - Straightforward corrections
I'll go from the simplest to the most complex. I'll also provide a more functional implementation in the end, without the while loop.
The construct var does not exist in ML. You probably meant val ind_score = ...
Array indexing is not done by array[i]. You need (as with everything else) a function to do that. The function happens to be List.nth. So, everywhere you have HW1[i], you should have List.nth(HW1, i).
Most language constructs expect a single expression, so you usually cannot simply string commands as we do in imperative languages. Thus, there are some constructs missing after the do in your while.
Variables in functional languages are usually immutable by default, so you have to indicate when you want something to be mutable. In your while, you want i to be mutable, so it has to be declared and used as such: val i = ref 0. When using the value, you have to use the syntax !i to get the 'current' value of the variable (essentially, de-referencing it).
The function call syntax in ML does not use (). When you call a function like score(a, b, c, d) what you are doing is creating a tuple (a, b, c, d) and passing it as a single argument to the function score. This is an important distinction because you are actually passing a tuple to your print function, which does not work because print expects a single argument of type string. By the way, the string concatenation operator is ^.
If you do all these changes, you'll get to the following definition of main. It is quite ugly but we will fix that soon:
val i = ref 0 (* Note that i's type is "int ref". Think about it as a pointer to an integer *)
val max = length(IDs)
fun main() =
while !i < max do (* Notice how annoying it is to always de-reference i and that the syntax to access a list element is not very convenient *)
let (* The 'let in end' block is the way to define values that will be used later *)
val ind_score = score(List.nth(HW1, !i), List.nth(HW2, !i), List.nth(HW3, !i), List.nth(Midterm, !i), List.nth(Final, !i))
val grade = letterGrade(ind_score)
in ( (* The parenthesis here allow stringing together a series of "imperative" operations *)
print(Int.toString(List.nth(IDs, !i)) ^ " " ^ List.nth(Names, !i) ^ " " ^ Int.toString(ind_score) ^ " " ^ grade ^ "\n");
i := !i + 1 (* Note the syntax := to re-define the value of i *)
)
end;
Part 2 - Making it more functional
Functional language programs are typically structured differently from imperative programs. A lot of small functions, pattern matching and recursion are typical. The code below is an example of how you could improve your main function (it is by no means "optimal" in terms of style though). A clear advantage of this implementation is that you do not even need to worry about the length of the lists. All you need to know is what to do when they are empty and when they are not.
(* First, define how to print the information of a single student.
Note that the function has several arguments, not a single argument that is a tuple *)
fun printStudent id name hw1 hw2 hw3 midterm final =
let
val ind_score = score (hw1, hw2, hw3, midterm, final)
val grade = letterGrade ind_score
in
print(Int.toString(id) ^ " " ^ name ^ " " ^ Int.toString(ind_score) ^ " " ^ grade ^ "\n")
end;
(* This uses pattern matching to disassemble the lists and print each element in order.
The first line matches an empty list on the first element (the others don't matter) and return (). Think of () as None in Python.
The second line disassemble each list in the first element and the rest of the list (first::rest), print the info about the student and recurse on the rest of the list.
*)
fun printAllStudents (nil, _, _, _, _, _, _) = ()
| printAllStudents (id::ids, name::names, hw1::hw1s, hw2::hw2s, hw3::hw3s, mid::midterms, final::finals) =
(printStudent id name hw1 hw2 hw3 mid final;
printAllStudents(ids, names, hw1s, hw2s, hw3s, midterms, finals));
printAllStudents(IDs, Names, HW1, HW2, HW3, Midterm, Final);
Note that it is a bit of a stretch to say that this implementation is more legible than the first one, even though it is slightly more generic. There is a way of improving it significantly though.
Part 3 - Using records
You may have noticed that there is a lot of repetition on the code above because we keep having to pass several lists and arguments. Also, if a new homework or test was added, several functions would have to be reworked. A way to avoid this is to use records, which work similarly to structs in C. The code below is a refactoring of the original code using a Student record. Note that, even though it has a slightly larger number of lines than your original code, it is (arguably) easier to understand and easier to update, if needed. The important part about records is that to access a field named field, you use an accessor function called #field:
(* Create a record type representing a student *)
type Student = {id:int, name:string, hw1:real, hw2:real, hw3:real, midterm:real, final:real};
(* Convenience function to construct a list of records from the individual lists of values *)
fun makeListStudents (nil, _, _, _, _, _, _) = nil (* if the input is empty, finish the list *)
| makeListStudents (id::ids, name::names, hw1::hw1s, hw2::hw2s, hw3::hw3s, mid::midterms, final::finals) = (* otherwise, add one record to the list and recurse *)
{id=id, name=name, hw1=hw1, hw2=hw2, hw3=hw3, midterm=mid, final=final} :: makeListStudents(ids, names, hw1s, hw2s, hw3s, midterms, finals);
val students = makeListStudents (IDs, Names, HW1, HW2, HW3, Midterm, Final);
fun score ({hw1, hw2, hw3, midterm, final, ...}: Student): int = (* Note the special patter matching syntax *)
round(hw1 * 0.1 + hw2 * 0.1 + hw3 * 0.1 + midterm * 0.3 + final * 0.4);
fun letterGrade (score) =
if score >= 90 then "A+"
else if score >= 85 then "A"
else if score >= 80 then "A-"
else if score >= 77 then "B+"
else if score >= 73 then "B"
else if score >= 70 then "B-"
else if score >= 67 then "C+"
else if score >= 63 then "C"
else if score >= 60 then "C-"
else if score >= 50 then "D"
else "E";
(* Note how this function became more legible *)
fun printStudent (st: Student) =
let
val ind_score = score(st)
val grade = letterGrade(ind_score)
in
print(Int.toString(#id(st)) ^ " " ^ #name(st) ^ " " ^ Int.toString(ind_score) ^ " " ^ grade ^ "\n")
end;
(* Note how, now that we have everything in a single list, we can use map *)
fun printAllStudents (students) = map printStudent students;
printAllStudents(students);

Related

Removing repeated statements from nested conditions in Ocaml

As an exercise in class, we are supposed to calculate the entry price for people in a night club, provided their age and gender. The under 25 get 20% off, and Females/NBs get 50% off, stacking multiplicatively.
While my code works, it repeats the gender check twice, which is poor form and could cause problems in a more complex application. How can I spare the repetition ?
(* OCaml Version *)
let entry_price age gender =
if age < 18
then (failwith "Must be over 18 to enter")
else let price = 12.0 in
if age <= 25
then let price = (price *. 0.8) in
if gender == 'f' || gender == 'x'
then (price *. 0.5)
else prix
else if gender == 'f' || gender == 'x'
then (price *. 0.5)
else price;;
Here is a Python version that does not repeat itself, thanks to ternary operators : (Could also be done with non-nested ifs, which Ocaml disallows)
# Python version
def entry_price(age : int, gender : str):
if age < 18:
raise ArgumentError("Must be over 18 to enter")
return (
12.0
*(0.8 if (age<25) else 1)
*(0.5 if gender in ['f', 'x'] else 1)
)
You can pretty much copy the python version:
let entry_price age gender =
if age < 18
then failwith "Must be over 18 to enter"
else
12.0
*. (if age < 25 then 0.8 else 1.0)
*. (if gender = 'f' || gender = 'x' then 0.5 else 1.0);;
There's only a slight difference in the if expression syntax, and you need to use the float-specific multiplication operator, *., instead of the int-specific *.
Also, to pick a nit, there are no statements in OCaml. Statements are an imperative/procedural construct, ie. do this, then do that. OCaml is an expression-based language, where each expression evaluates to a value. You can still discard the value of an expression, and thereby make it look like a statement, but the compiler will complain unless you are very explicit about discarding it since that is usually a user error.
#glennsl provides a nice direct translation of the Python code to OCaml, but there are other ways we can approach this.
If we create a variant type to describe ages, we can write a categorize_age function which will translate a numeric age to a descriptive term we can pattern match on. It also gives us a convenient place in the future to change what ages we use to make these determinations.
Then we can use a match on both age and gender to consider the four possible outcomes:
Under 18
Under 25 and either 'f' or 'x' gender
25 or older and either 'f' or 'x' gender
Anyone else 18 or older
type age_cat = Under_age | Legal | Adult
let categorize_age a =
if a < 18 then Under_age
else if a <= 25 then Legal
else Adult
let entry_price age gender =
let price = 12.0 in
match categorize_age age, gender with
| Under_age, _ -> failwith "Must be over 18 to enter"
| Legal, ('f' | 'x') -> price *. 0.8 *. 0.5 (* or just 0.4 *)
| Adult, ('f' | 'x') -> price *. 0.5 *. 0.5 (* or just 0.25 *)
| _ -> price
Looking at the logic here, we actually don't need to specify Adult, ('f' | 'x') because we know that we've already matched cases where when gender is 'f' or 'x', age is either Under_age or Legal. Thus we can use a wildcard _ for the pattern: _, ('f' | 'x').
The last pattern is a wildcard because we're matching any case that hasn't already been matched, and those values are not relevant to the outcome, so there's no point in binding name(s) to them.
let entry_price age gender =
let price = 12.0 in
match categorize_age age, gender with
| Under_age, _ -> failwith "Must be over 18 to enter"
| Legal, ('f' | 'x') -> price *. 0.8 *. 0.5 (* or just 0.4 *)
| _, ('f' | 'x') -> price *. 0.5 *. 0.5 (* or just 0.25 *)
| _ -> price

How to transfer constraints of string type to z3 solver expr in C++?

For example, I got a constraint: "(number < 10) && (name == "hello")";
I can do the following now:
context c;
expr number= c.int_const(number);
expr name = c->string_val(name.c_str());
expr constrain = ***procedure***("(number < 10) && (name == \"hello\")");
How can I implement this procedure()?
there is an incomplete and unproofed answer in Use a C++ string in z3::expr?, and I still can not figure out how to implement it?
I am very eagered and appreciateed for your help! Thanks!
Try:
#include <z3++.h>
using namespace z3;
using namespace std;
int main ()
{
context c;
expr number = c.int_const("number");
expr name = c.constant(c.str_symbol("name"), c.string_sort());
expr hello = c.string_val("hello");
expr constraint = number < 10 && name == hello;
solver s(c);
s.add(constraint);
cout << s.check() << "\n";
cout << s.get_model() << "\n";
return 0;
};
Assuming you put the above in a file called a.cpp, you compile it like this:
$ g++ -std=c++11 a.cpp -l z3
And when run, it produces:
sat
(define-fun number () Int
9)
(define-fun name () String
"hello")
Using higher-level APIs
As you no doubt noticed, programming z3 in C/C++ is very verbose and terribly error prone. Unless you've some other reason to use C/C++, I'd recommend using a higher-level API, such as Python or Haskell, which simplifies programming in z3 to a great extent.
Python
For instance, you'd code your problem in Python like this:
from z3 import *
number = Int('number')
name = String('name')
s = Solver()
s.add(number < 10, name == "hello")
print(s.check())
print(s.model())
producing:
sat
[number = 9, name = "hello"]
Haskell
And in Haskell, it would look like:
import Data.SBV
ex :: IO SatResult
ex = sat $ do number <- sInteger "number"
name <- sString "name"
constrain $ number .< 10 .&& name .== literal "hello"
producing:
*Main> ex
Satisfiable. Model:
number = 9 :: Integer
name = "hello" :: String
Summary
Long story short, programming z3 in C/C++—while entirely possible—is something that's best avoided if you can use a higher-level interface. If you must stick to C/C++, be sure to study the API: https://z3prover.github.io/api/html/namespacez3.html

Check if (number) plus (number) equals the answer you type

Basically I'm trying to make a .vbs file that gives you ten random addition problems. Then, in an input box, you type the answer to the given problem. Everything works except the If/Else statement that tells you if you are right or wrong. If it says "What is 2 + 2" and I type "4" (without quotations), then it outputs "(You attack) TRIP! POW! ouchy wrong You could've gotten it right IF you typed: 4." Here's the code:
msgbox("Starting addition. Press OK to begin.")
dim i
i = 0
Do
i = i + 1
'i is for the question timer
Dim max,min
max=100 'max random
min=1 'min random
dim j, k, l 'part part total
Randomize
msgbox("What is")
j = Int((max-min+1)*Rnd+min)
msgbox(j)
msgbox("plus")
k = Int((max-min+1)*Rnd+min)
msgbox(k)
answer = Inputbox("I hope you got all of that... ^_^")
l = j + k
if answer = l then
msgbox("(You attack) BAM! Right on target")
else
msgbox("(You attack) TRIP! POW! ouchy wrong")
msgbox("You could've gotten it right IF you typed:")
msgbox(l)
end if
loop until i = 10
Read in Comparison Operators (VBScript) reference:
... how expressions are compared or what results from the comparison,
depending on the underlying subtype: ...
If one expression is numeric and the other is a string Then the numeric expression is less than the string expression.
j = Int((max-min+1)*Rnd+min)
k = Int((max-min+1)*Rnd+min)
answer = Inputbox("I hope you got all of that... ^_^" _
& vbCR & "What is " & j & " plus " & k)
l = j + k
If IsNumeric( answer) then
answer = Int( answer)
Else
answer = l - 1
End If
if answer = l then
msgbox("(You attack) BAM! Right on target")
else
msgbox("(You attack) TRIP! POW! ouchy wrong")
msgbox("You could've gotten it right IF you typed:")
msgbox(l)
end if

Understanding Recursive Function

I'm working through the book NLP with Python, and I came across this example from an 'advanced' section. I'd appreciate help understanding how it works. The function computes all possibilities of a number of syllables to reach a 'meter' length n. Short syllables "S" take up one unit of length, while long syllables "L" take up two units of length. So, for a meter length of 4, the return statement looks like this:
['SSSS', 'SSL', 'SLS', 'LSS', 'LL']
The function:
def virahanka1(n):
if n == 0:
return [""]
elif n == 1:
return ["S"]
else:
s = ["S" + prosody for prosody in virahanka1(n-1)]
l = ["L" + prosody for prosody in virahanka1(n-2)]
return s + l
The part I don't understand is how the 'SSL', 'SLS', and 'LSS' matches are made, if s and l are separate lists. Also in the line "for prosody in virahanka1(n-1)," what is prosody? Is it what the function is returning each time? I'm trying to think through it step by step but I'm not getting anywhere. Thanks in advance for your help!
Adrian
Let's just build the function from scratch. That's a good way to understand it thoroughly.
Suppose then that we want a recursive function to enumerate every combination of Ls and Ss to make a given meter length n. Let's just consider some simple cases:
n = 0: Only way to do this is with an empty string.
n = 1: Only way to do this is with a single S.
n = 2: You can do it with a single L, or two Ss.
n = 3: LS, SL, SSS.
Now, think about how you might build the answer for n = 4 given the above data. Well, the answer would either involve adding an S to a meter length of 3, or adding an L to a meter length of 2. So, the answer in this case would be LL, LSS from n = 2 and SLS, SSL, SSSS from n = 3. You can check that this is all possible combinations. We can also see that n = 2 and n = 3 can be obtained from n = 0,1 and n=1,2 similarly, so we don't need to special-case them.
Generally, then, for n ≥ 2, you can derive the strings for length n by looking at strings of length n-1 and length n-2.
Then, the answer is obvious:
if n = 0, return just an empty string
if n = 1, return a single S
otherwise, return the result of adding an S to all strings of meter length n-1, combined with the result of adding an L to all strings of meter length n-2.
By the way, the function as written is a bit inefficient because it recalculates a lot of values. That would make it very slow if you asked for e.g. n = 30. You can make it faster very easily by using the new lru_cache from Python 3.3:
#lru_cache(maxsize=None)
def virahanka1(n):
...
This caches results for each n, making it much faster.
I tried to melt my brain. I added print statements to explain to me what was happening. I think the most confusing part about recursive calls is that it seems to go into the call forward but come out backwards, as you may see with the prints when you run the following code;
def virahanka1(n):
if n == 4:
print 'Lets Begin for ', n
else:
print 'recursive call for ', n, '\n'
if n == 0:
print 'n = 0 so adding "" to below'
return [""]
elif n == 1:
print 'n = 1 so returning S for below'
return ["S"]
else:
print 'next recursivly call ' + str(n) + '-1 for S'
s = ["S" + prosody for prosody in virahanka1(n-1)]
print '"S" + each string in s equals', s
if n == 4:
print '**Above is the result for s**'
print 'n =',n,'\n', 'next recursivly call ' + str(n) + '-2 for L'
l = ["L" + prosody for prosody in virahanka1(n-2)]
print '\t','what was returned + each string in l now equals', l
if n == 4:
print '**Above is the result for l**','\n','**Below is the end result of s + l**'
print 'returning s + l',s+l,'for below', '\n','='*70
return s + l
virahanka1(4)
Still confusing for me, but with this and Jocke's elegant explanation, I think I can understand what is going on.
How about you?
Below is what the code above produces;
Lets Begin for 4
next recursivly call 4-1 for S
recursive call for 3
next recursivly call 3-1 for S
recursive call for 2
next recursivly call 2-1 for S
recursive call for 1
n = 1 so returning S for below
"S" + each string in s equals ['SS']
n = 2
next recursivly call 2-2 for L
recursive call for 0
n = 0 so adding "" to below
what was returned + each string in l now equals ['L']
returning s + l ['SS', 'L'] for below
======================================================================
"S" + each string in s equals ['SSS', 'SL']
n = 3
next recursivly call 3-2 for L
recursive call for 1
n = 1 so returning S for below
what was returned + each string in l now equals ['LS']
returning s + l ['SSS', 'SL', 'LS'] for below
======================================================================
"S" + each string in s equals ['SSSS', 'SSL', 'SLS']
**Above is the result for s**
n = 4
next recursivly call 4-2 for L
recursive call for 2
next recursivly call 2-1 for S
recursive call for 1
n = 1 so returning S for below
"S" + each string in s equals ['SS']
n = 2
next recursivly call 2-2 for L
recursive call for 0
n = 0 so adding "" to below
what was returned + each string in l now equals ['L']
returning s + l ['SS', 'L'] for below
======================================================================
what was returned + each string in l now equals ['LSS', 'LL']
**Above is the result for l**
**Below is the end result of s + l**
returning s + l ['SSSS', 'SSL', 'SLS', 'LSS', 'LL'] for below
======================================================================
This function says that:
virakhanka1(n) is the same as [""] when n is zero, ["S"] when n is 1, and s + l otherwise.
Where s is the same as the result of "S" prepended to each elements in the resulting list of virahanka1(n - 1), and l the same as "L" prepended to the elements of virahanka1(n - 2).
So the computation would be:
When n is 0:
[""]
When n is 1:
["S"]
When n is 2:
s = ["S" + "S"]
l = ["L" + ""]
s + l = ["SS", "L"]
When n is 3:
s = ["S" + "SS", "S" + "L"]
l = ["L" + "S"]
s + l = ["SSS", "SL", "LS"]
When n is 4:
s = ["S" + "SSS", "S" + "SL", "S" + "LS"]
l = ["L" + "SS", "L" + "L"]
s + l = ['SSSS", "SSL", "SLS", "LSS", "LL"]
And there you have it, step by step.
You need to know the results of the other function calls in order to calculate the final value, which can be pretty messy to do manually as you can see. It is important though that you do not try to think recursively in your head. This would cause your mind to melt. I described the function in words, so that you can see that these kind of functions is are descriptions, and not a sequence of commands.
The prosody you see, that is a part of s and l definitions, are variables. They are used in a list-comprehension, which is a way of building lists. I've described earlier how this list is built.

Error code in let-in expression

I have this SML code. I don't know why I cannot compile this :
fun score =
let
val sum = 3; (* error at this line : SYNTAX ERROR : inserting LPAREN *)
if sum div 2 > 0
then sum = 0
else sum = 1
(*some other code*)
in
sum (* I want to return sum after some steps of calculation *)
end
There are more issues with your code, than jacobm points out.
You are also missing a function argument. Functions in SML always takes one argument. For example
fun score () =
let val sum = 3
val sum = if sum div 2 > 0
then sum = 0
else sum = 1
in
sum
end
However this still doesn't make much sense. since the expressions sum = 0 and sum = 1 evaluates to a Boolean.
A let-expression is used to make some local declarations which are only visible inside the in ... end part. Thus the calculations you wan't to do with sum, should probably be done inside the in ... end part, unless you wan't to express it as a means of a function.
One such example is
fun score () =
let val sum = 3
in
if sum div 2 > 0
then ...
else ...
end
If we look at the syntax of a let-expression, it probably makes more sense
let
<declaration>
in
<expr> ; ... ; <expr>
end
Since if-then-else is an expression, it can't be in the "declarations part" by itself.
That syntax just isn't legal -- in between let and in all you're allowed to have is a series of val name = expr fragments. You can do this, though:
fun score =
let val sum = 3
val sum = if sum div 2 > 0
then sum = 0
else sum = 1
in
sum
end
I would consider it a bit of a weird style to use sum for both variable names, but it's legal.