SML: pattern with constants? - sml

I am trying to write a pattern matching function in SML but I get this error
Error: non-constructor applied to argument in pattern: -
I am trying to do something as such:
fun illegal (c:char) i j n m =
let
fun legal (#"A") 0 j = true
|legal (#"B") i 0 = true
|legal (#"C") i n = true
|legal (#"D") m j = true
| legal c i j = false
in
if (legal c i j = false) then 0
else i
end
**I suspect that the problem is that my n,m are two constants that have been valued right before. Any help appreciated
(I searched online and here I added parentheses in my chars, and tried some other stuff but my error persisted)**

When you pattern match, something like i is not checking to see if it has the same value as the existing i binding, but it's introducing a new i binding.
When you have:
legal (#"A") 0 j = true
You're really looking for is something like:
legal (#"A") 0 j2 = j = j2

Related

'int' object is not iterable - while not loop with booleans

I have the following problem:
I want to use a while not loop under a true-false condition. I define the condition and tell the loop what to do while the condition is True and False respectively. However, in the line 'converge = False, j = 1' (please see code below), I get the message 'int' object is not iterable. Can you help please? I don't know what the problem here is as I am fairly new to python.
Condition that the model run should stop at steady state (when the slope does not change anymore)
Final_pools_2 = [pools_2[-1,:]]
converge = False, j = 1
while not converge:
parset_2[-6:] = Final_pools_2
j = j+1
fluxes_2,pools_2 = f2py.dalec(fluxes_2,pools_2,drivers,-10.,deltat,removal,fires,parset_2,1)
Final_pools_2.append(pools_2[-1,:])
test = 1-np.abs(Final_pools_2[-1]/parset_2[-6:])
if test > 0.001:
converge = True
Final_pools_2 = np.array(Final_pools_2)
Thanks for your help :)
You cant inialise two variables of different types on one line.
Change it to:
converge = False
j = 1

List of Tuples remove error

ExpenseL is my list of tuples and I try to remove from the list starting from start to stop but I just get this error: in removeFromAtoB
expenseL.pop(i)
TypeError: 'tuple' object cannot be interpreted as an integer
Please help me!!! :)
def removeFromAtoB():
aux = copy.deepcopy(expenseL)
print(expenseL, "\n")
start = int(input("Starting point: "))
stop = int(input("Ending point: "))
j = 0
for i in expenseL:
if j >= start and j <= stop:
expenseL.pop(i)
j += 1
print(expenseL)
You're iterating over your list of tuples:
for i in expenseL
That means i will be one of those tuples. Then you try to use it in list.pop:
expenseL.pop(i)
This won't work, because list.pop expects an index. Just enumerate your list:
for index, tpl in enumerate(expenseL):
...
expenseL.pop(index)
But this breaks, too, because the indices change when you remove an element. You could circumvent that by not increasing j in that case, but the simpler way is just assigning an empty list to the slice:
def removeFromAtoB():
start = int(input("Starting point: "))
stop = int(input("Ending point: "))
expenseL[start:stop+1] = []

Compress Python code

How can I write the following loop in one line? Or is this not possible because of the if-statement?
a = listWithFancyIntegers
for i in range(len(a)):
if a[i] < 0:
a[i] = 0
else:
a[i] = 1
What I do not want to have is a list of booleans.
I have already searched the web to check if I can use something like a Lambda expression but I didn't find anything that helped me. (Or I didn't understand it :D)
Thank you for your support.
a = [0 if n < 0 else 1 for n in listWithFancyIntegers]
EDIT
I prefer the code I wrote above, but here's another way:
a = [int(n >= 0) for n in listWithFancyIntegers]
or if you prefer map to list comprehension:
a = map(lambda n: int(n >= 0), listWithFancyIntegers)
This can be done in a single line in Python
a = [0 if i < 0 else 1 for i in a]

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

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.