I am fresh on SML and doing a homework by that. "Write a function number_in_month that takes a list of dates and a month (i.e., an int) and returns how many dates in the list are in the given month."
That's what I worked out and cannot see anything wrong with it. Please help.
`
fun number_in_month (dates: (int*int*int) list,month:int) =
if ((#2 (hd dates)) = month)
then val flag=1 flag+number_in_month(tl dates, month)
else number_in_month((tl dates),month)`
REPL tells that: replacing VAL with EQUALOP.
You can't bind variables "that way". A binding of a variable is a declaration and thus cannot be done where an expression is expected.
In this case you have to use a let-in-end expression
fun foo x =
let
val a = 42
in
a*x
end
Your problem is endless recursion. Compiler can't get out of it because independ of result if..then..else you're running your function again
Try this:
fun number_in_month (dates: (int*int*int) list,month:int) =
if null dates
then 0
else if ((#2 (hd dates)) = month)
then val flag=1 flag+number_in_month(tl dates, month)
else number_in_month((tl dates),month)
I tried to fix it by myself and that was my solution:
fun number_in_month (dias: (int*int*int) list,mes:int) =
if null dias
then 0
else if ((#2 (hd dias)) = mes)
then let val flag = 1 + number_in_month(tl dias, mes)
in flag
end
else number_in_month((tl dias),mes)
I hope you can also use it!
The error message from REPL is confusing, but Jesper is right that you should use let-in-end expression, if you need assignment statement in functions. That will surely get you that error resolved.
Related
I am new to scala. Pls be gentle. My problem for the moment is the syntax error.
(But my ultimate goal is to print each group of 3 characters from every string in the list...now i am merely printing the first 3 characters of every string)
def do_stuff():Unit = {
val s = List[String]("abc", "fds", "654444654")
for {
i <- s.indices
r <- 0 to s(i).length by 3
println(s(i).substring(0,3))
} yield {s(i)}
}
do_stuff()
i am getting this error. it is syntax related, but i dont undersatnd..
Error:(12, 18) ')' expected but '.' found.
println(s(i).substring(0,3))
That code doesn't compile because in a for-comprehension, you can't just put a print statement, you always need an assignment, in this case, a dummy one can solve your porblem.
_ = println(s(i).substring(0,3))
EDIT
If you want the combination of 3 elements in every String you can use combinations method from collections.
List("abc", "fds", "654444654").flatMap(_.combinations(3).toList)
New to Scala and see people are using sign f ahead of a string, here is an example I tried which works. Wondering what is the function of sign f? Does it need to be combined to use with %s? Tried to search some tutorials but failed. Thanks.
object HelloWorld {
def main(args: Array[String]) {
var start = "Monday";
var end = "Friday";
var palindrome = "Dot saw I was Tod";
println(f"date >= $start%s and date <= $end%s" + palindrome);
// output date >= Monday and date <= FridayDot saw I was Tod
}
}
http://docs.scala-lang.org/overviews/core/string-interpolation.html
The f Interpolator
Prepending f to any string literal allows the creation of simple
formatted strings, similar to printf in other languages. When using
the f interpolator, all variable references should be followed by a
printf-style format string, like %d.
PS. another somewhat related feature is http://docs.scala-lang.org/overviews/quasiquotes/expression-details
See the explanation here. For people coming from C the f interpolator is a printf style formatter. % is to denote the type of data and with a $ you may may refer to a previously defined variable.
The % in not mandatory. Its just that you will get a format that is decided by the compiler at compile time. Bit uyou may want to change the output format sometimes.
So if i take an example ,
var start = "Monday";
var end = "Friday";
val age = 33
var palindrome = "Dot saw I was Tod";
println(f"date >= $start and date <= $end and age<= $age%f" + palindrome);
I could omit the %f and i will see a output of 33 as it will inferred as Int. However i could use %f if i wanted to format it as a float. Also if you use a incompatible formatted you will receive a error at compile time.
I am new to SML and don't know how to use the "or" operator in an if statement. I would be really grateful if someone explains it to me, since I have checked multiple sources and nothing seems to work.
Thank you !
In SML, logical or is called orelse and logical and is called andalso.
As an example
if x = 2 orelse x = 3 orelse x = 5
then print "x is a prime"
else print "x is not a prime (also, I don't believe in primes > 5; please respect my beliefs)"
I am Scala beginner. I could not understand the Scala expression below :
x = if(true) x+1 else x-1
Well the initial value of x is 3, after executing the above expression it becomes 4.
I am not able to understand if(true), what is it actually evalutating ?
if(true) x else y will always execute the if branch because the condition is true. Thus, the result of your if expression will be always x + 1.
With a nod to #TillRohrmann, for us Scala newbies, things are sometimes not as clear as they are to seasoned programmers. Here is my attempt at detail, not just for you but for others who might have a similar question in future.
x = if (true) x+1 else x-1
Think of your expression as
x = function_if ( argument=true ) {
if (argument = true ){
return x+1
}else{
return x-1
}
}
Since the argument is always true, the answer is always x+1.
Hope this helps!
I have the following function that aims to set a colour to a specific pixel.
function val = xyz(p)
if (p(2,2)) == 40
val=[255,0,0];
end
end
I'm not sure if the function works correct in assigning the colour since I get the following error when I call the function:
Output argument "val" (and maybe others) not assigned during call to.....
How can I solve this issue?
Thanks.
Sebastian said right, you need to add an else to be sure your output is fill.
function val = xyz(p)
if (p(2,2)) == 40
val=[255,0,0];
else
val = [];
end
end