Simple SML code error - sml

I have just started learning SML and still in the process of making sense of its error messages.
when trying to input the function definition below
val rec : real->real = fn 0.0 => 0.0 | n:real => 1.0/n;
i get the following error :
stdIn:25.9-25.17 Error: syntax error: deleting COLON ID ARROW
stdIn:25.24-25.33 Error: syntax error: deleting FN REAL DARROW
stdIn:25.38 Error: syntax error found at BAR
can someone point out what i am doing wrong ?
thank you.

You have two errors in your code:
Between val rec and the type annotation there should be the name of the value you're defining.
You can't use pattern matching on reals. Since reals are inexact, they aren't equality types, so you can't use = on them either. You need to use Real.== to compare reals for equality (or better: don't compare them for equality, but compare them against some delta instead).

Related

SML getting an unbound variable or constructor error when everything seems right

I'm trying to figure out mutual recursion. I have this code:
fun take(L)=
if L=nil then nil
else hd(L) :: skip(tl(L))
AND
fun skip(L)=
if L=nil then nil
else take(tl(L));
but it gives me these errors:
stdIn:54.14-54.18 Error: unbound variable or constructor: skip
stdIn:55.1-55.4 Error: unbound variable or constructor: AND
What am I doing wrong?
Your immediate error is because Standard ML is case-sensitive, and all of its reserved words are in lowercase; so you need to write and rather than AND.
Additionally, fun introduces an entire declaration, not an individual binding, meaning that you need to remove the extra fun after and.
Lastly, your functions currently require the list to have an equality type (such as int list or string list), which may not be a deal-breaker, but given what the functions actually do, there's really no reason they can't support non-equality types such as real list. To achieve that, you should match the parameter against the pattern nil, instead of testing whether the parameter equals nil. (More generally, you should use pattern-matching in more places; you have no reason to call hd and tl.)
Putting it together:
fun take nil = nil
| take (h::t) = h :: skip t
and skip nil = nil
| skip (h::t) = take t

Problem with sort in scala, got "Diverging implicit expansion ....." error. Sorting a list of tuples based on its first element but in reverse order

I am trying to sort a list of tuples in Scala, the following code will result in error:
List("a"->1,"b"->2, "c"->3).sortBy(-_._1)
error: diverging implicit expansion for type scala.math.Ordering[B]
starting with method Tuple9 in object Ordering
List("a"->1,"b"->2, "c"->3).sortBy(-_._1)
^
but the code below works just fine:
List("a"->1,"b"->2, "c"->3).sortBy(_._1)
res39: List[(String, Int)] = List((a,1), (b,2), (c,3))
The only difference is the negative sign in sortBy!
What is the problem?
Since there is no such thing as a negative String, you can't sort by it. You can reverse-sort element types that can't be negated, either by reversing the sorted results...
List("a"->1, "b"->2, "c"->3).sortBy(_._1).reverse
...or by replacing the implicit Ordering with an explicit reversed Ordering.
List("a"->1, "b"->2, "c"->3).sortBy(_._1)(Ordering[String].reverse)
The error occurs, because - method is not defined on String. The following works just fine:
List("a"->1, "b"->2, "c"->3).sortBy(-_._2)
It's because - is defined for Int.
Maybe you meant something like:
List("a"->1, "b"->2, "c"->3).sortBy(-_._1.length)

F# if statement function with parameter syntax

The issue is: I cannot figure out what the error is refering to when it diplays
Here is the error:
source_file.fs(10,5): error FS0010: Unexpected keyword 'if' in binding. Expected '=' or other token.
And I've been researching this error and proper syntax for a good while.
Now what I want to do, I hope, is obvious from the general look of the program.
Knowing the correct syntax would be great as microsofts docs are not great.
Seeing as this is case, I just don't understand what could be wrong.
open System
let one = "one"
let two = "two"
if oneortwo one then printfn one + " 1"
else printfn two + " 2"
let oneortwo(a : string)
if a = "one" then return true
elif a = "two" then return false
return false
F# is an expression based language, which means that everything has a value (returns something). F# is also statically typed, so everything returned is of a specific type.
Since everything is an expression, the return keyword is not used. The final expression in a function body is the returned value.
This goes also for if ... then ... else: every branch must return a value and be of the same type.
The correct syntax for your function is
let oneortwo a =
if a = "one" then true
else false
An excellent source of learning F# is Scott Wlaschin's site F# for fun and profit

Error: This expression has type unit but an expression was expected of type inst list

|ScmOp(g,h) ->newList := (createInstList **!newList** a currentReg);
This is our code, we received the above error when compiling. The error comes from the !newList parameter we send to the recursive function. newList is defined as a reference so we sent !newList in order to pass its value. When removing the ! we received an error that we sent a ref for inst list instead of an inst list (eventually we want to send an inst list!).
OK, I think the problem is that the compiler thinks createInstList returns unit. This is due to precedence of ;, I think.
It looks to me like the !newlist at the end is supposed to be the result of the whole createInstList function. But I suspect that it's being treated as part of the inner match.
You might try this:
let rec createInstList instList expr currentReg =
let newList = ref instList in
(match
...
done);
!newList

How to fix errors in my Fortran code

I am not familiar with errors in Fortran. Could you please explain what are these errors?
int.f90(18): error #5082: Syntax error, found IDENTIFIER 'K' when expecting one of: ( % : . = =>
do k=0.0 to m
---^
int.f90(19): error #5082: Syntax error, found IDENTIFIER 'Q' when expecting one of: ( % : . = =>
do q=1 to m-1
----^
int.f90(20): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ) ,
f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
----------------------------------------------^
int.f90(24): error #5082: Syntax error, found IDENTIFIER 'I' when expecting one of: ( % : . = =>
do i=1 to m-1
----^
int.f90(4): error #6406: Conflicting attributes or multiple declaration of name. [INT]
function int(f,a,b,int,m)
-------------------^
int.f90(9): error #6418: This name has already been assigned a data type. [M]
integer:: i,m
------------^
int.f90(10): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [PI]
real,parameter:: pi,pi=3.14
-----------------^
int.f90(10): error #6418: This name has already been assigned a data type. [PI]
real,parameter:: pi,pi=3.14
--------------------^
int.f90(11): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [EPS]
real,parameter:: eps,eps=1.89
-----------------^
int.f90(11): error #6418: This name has already been assigned a data type. [EPS]
real,parameter:: eps,eps=1.89
---------------------^
int.f90(12): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [E]
real,parameter:: e,e=1.602*((10)**(-19))
-----------------^
int.f90(12): error #6418: This name has already been assigned a data type. [E]
real,parameter:: e,e=1.602*((10)**(-19))
-------------------^
int.f90(21): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
int.f90(23): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
int.f90(26): error #6410: This name has not been declared as an array or a function. [F2]
s=2*f2(t)+4*f2(t+h)
-----^
int.f90(27): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
and this is my code:
function int(f,a,b,int,m)
implicite none
double precision f1,f2,a,b,m,int,s
double precision h,t
integer:: k,q
integer:: i,m
real,parameter:: pi,pi=3.14
real,parameter:: eps,eps=1.89
real,parameter:: e,e=1.602*((10)**(-19))
a=0.0
b=pi
m=150
s=0.0
h=(b-a)/m
do k=0.0 to m
do q=1 to m-1
f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
end do
f2=((2*pi*(e**2))/eps)*f
end do
do i=1 to m-1
t=a+(i*h)
s=2*f2(t)+4*f2(t+h)
end do
int=(h/3)*(s+f2(a)+f2(b)+4*f2(a+h))
print*,int
return
end function int
Your errors are:
Your loop-control in every one of your do loops is malformed. (Fortran 2008 Cl. 8.1.6.2 R818)
DO loops are specified as (neglecting optional syntax):
do variable=start, end
Note the comma rather than the word to.
Parenthesis must be balanced. (See Fortran 2008 7.1.2.2 R701 ( expr ))
Every left parenthesis ( must have a corresponding right parenthesis ). Your statements assigning f1 and f2 both have more left parenthesis than right parenthesis.
Declaring a variable twice in the same scoping unit is wrong. (Fortran 2008 Cl. 16.3.1 paragraph 3, see also: Cl. 5.2)
To delcare a variable and initialize it, you need only use its name once
real, parameter :: pi=3.14
You call the function int and a dummy variable int. You need to use different names for these.
You declare m as both integer and double precision. You can only declare it as one type, not two.
Of note:
Fix errors starting with the first ones, as later ones can be caused by the compiler throwing out earlier bad statements and will go away when those are fixed.
Real values in do loop variables are deleted in the latest standard, and should be avoided in new code. (Fortran 2008 Annex B.1 2(1)).
Your code demonstrates a lack of understanding in Fortran and you will benefit greatly from finding a Fortran tutorial that covers the basics of variables and loops.