SML - Errors in code - syntax-error

I'm making an SML lambda calculus translation function and I have run into several errors and I don't know how to fix it.
cfunf id (CAPP(e1,e2))=
if not(cfree id (CAPP(e1,e2)))
then CAPP(CK,CAPP(e1,e2))
else
if ((CID id) = e2) andalso not(cfree id e1)
then e1
else CAPP(CAPP(CS, (cfunf id e1)),(cfunf id e2));
The errors I get are RPAREN THEN and ELSE IDA. I'm pretty sure RPAREN is for an unbalance in parenthesis but there is none I can see. This code is required for the following function. I'm sure it some simple syntax error but I have no idea how too proceed so any help will be appreciated.

The line with the second if has 3 opening but 4 closing parentheses. (In fact only one pair of parens is actually needed there.)

You added and extra parenthesis. Remove the last ) from
if ((CID id) = e2) andalso not(cfree id e1))
So it becomes
if ((CID id) = e2) andalso not(cfree id e1)

Related

null safety without elvis operator kotlin

a ?: b is just shorthand for if (a != null) a else b.
I understand that ?: eliminates the danger of null references from code. However I encountered fragment, which I failed to re-write myself. Here it is:
`
val words = mutableMapOf<String, Int>()
input = scan.next()
wordCounts[input] = (wordCounts[input] ?: 0) + 1
What I tried is:
words[input] = if (words[input] != null) (words[input]+1) else 0
However Intelij displays error here
After I replaced it with its sugesttion I have further errors:
words[input] = if (words[input] != null) (words[input]?.plus(1)) else 0
How can I fix that if statement remain?
val n = words[input]
words[input] = if (n != null) n + 1 else 0
words might change value to become null after your if statement, so you should capture the variable first and then do the assignment.
a ?: b is just shorthand for if (a != null) a else b
That's almost true.  But there's one subtle difference: the elvis version only evaluates a once, while the other evaluates it twice — carrying the possibility that it could give a different result each time.
In this case, a is words[input].  What if another thread changes the contents of words in between the two evaluations, removing the value for key input so that words[input] returns null the second time?  (Even if you know that no other thread could possibly be accessing it, if the compiler can't prove that, it has to assume that the second time could give a null result.)
So the solution is to evaluate it only once.
You can do that with a temporary variable, as per another answer.  But Kotlin's scoping functions give you a slightly simpler option:
words[input].let{ if (it != null) it + 1 else 1 }
This works because let() is an extension method defined on Any?, so (unlike ‘real’ methods) it's safe to call on a null.
However, this just illustrates why the elvis operator is useful, and how it simplifies some code!  You don't explain why you want to rewrite your code to avoid it; that could be a useful learning exercise, but in practice you'd just use the elvis operator instead.

How can I get integer output from a sql execution using F#

I am new in F#. So that I face some problem in my project.
let getTotalNoOfSmsPerDay (Db cStr) =
use cmd = new SqlCommandProvider<"select COUNT(id) as NoOfSMS
from transport.SmsNotification
where CONVERT(date, CreatedOn) = CONVERT(date, GETDATE())
",ConnectionString, SingleRow = true>(cStr)
async {
let! result = cmd.AsyncExecute()
return result
}
This is my Code . I just want a integer number NoOfSMS . But It gives me <Option<int>>
How can I solve this?
Thanks in advance
Option represents that nothing may be returned. This is the point where most other languages would return null.
There are a few ways to use the value inside Option. Explore the functions available.
Often what you will do though is to match on the 2 cases ie. Some value was returned or None.
match result with
| Some v -> v
| None -> 0
Here are some links to tutorials I have written on the topics.
Control flow
Pattern matching
Handling no data

two similar pg_query statements, one is OK, the other FAILS. Why?

I am having a confusion on what is happening. So I just need some ideas or clarifications.
I have 3 PHP variables and two PostgreSQL queries:
$date_start = $d1 = "2013-01-01";
$date_end = $d2 = "2013-01-05";
$car = "x";
$query_1 = pg_query($con, "SELECT AVG(longitude) as lon, AVG(latitude) as lat, MAX(longitude) as maxlon, MIN(longitude) as minlon, MAX(latitude) as maxlat, MIN(latitude) as minlat FROM table WHERE car='".$car."' AND testdate>='".$date_start."' AND testdate<='".$date_end."'";
$query_2 = pg_query($con, "SELECT MIN(testtime) as t1, MAX(testtime) as t2 FROM table WHERE car='$car' AND testdate>='$d1' AND testdate=<'$d2' GROUP BY testdate");
The trouble here is that $query_1 gets through fine and gives me a proper result resource, while the other one is always false with an error message:
ERROR: operator does not exist: date =< unknown
LINE 1: ... car='BM1' AND testdate>='2013-04-04' AND testdate=<'2013-04...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
So... any ideas what is happening there? I am sure the queries are almost the same, but for the reason I do not understand PostgreSQL is complaining about one but not the other!
I do not need help on how to fix this, I just need to know the reason. Why the behaviour is different..
Cheers!!
Yours
Andy
I am not a postgress expert but AFAIK there is no =< operator but <=
$query_2 = pg_query($con, "SELECT MIN(testtime) as t1, MAX(testtime) as t2 FROM table WHERE car='$car' AND testdate>='$d1' AND testdate=<'$d2' GROUP BY testdate");

'numpy.float64' object is not callable

I get the error as in the title of my post. I have seen this come up in other questions, but I am interested in understanding what this means since the other answers were in a specific context that does not apply to me.
Secondly, I would like to understand how this applies to my code, shown below. Note that this all works OK if Zindx = 0, but not for any other case.
Zindx = list(E).index(0)
for m in range(0,N):
if m != Zindx:
for n in range(0,N):
if n != Zindx:
if n != m:
x[m,m] = x[m,m] (
- (E[n]-E[m] + E[n])*x[m,n]*x[n,Zindx]
/x[m,Zindx]/E[m]
)
This:
x[m,m] (
- (E[n]-E[m] + E[n])*x[m,n]*x[n,Zindx]
/x[m,Zindx]/E[m]
)
Is trying to call x[m,m] as a function with the expression within the parenthesis as an argument. I am guessing x[m,m] returns a float.
Do you mean to multiply x[m,m] by the term in the parenthesis? If so, add the *.

Nhibernate query condition within sum

I am trying the following code but nhibernate is throwing the following exception:
Expression type 'NhSumExpression' is not supported by this SelectClauseVisitor.
var data =
(
from a in session.Query<Activity>()
where a.Date.Date >= dateFrom.Date && a.Date.Date <= dateTo.Date
group a by new { Date = a.Date.Date, UserId = a.RegisteredUser.ExternalId } into grp
select new ActivityData()
{
UserID = grp.Key.UserId,
Date = grp.Key.Date,
Bet = grp.Sum(a => a.Amount < 0 ? (a.Amount * -1) : 0),
Won = grp.Sum(a => a.Amount > 0 ? (a.Amount) : 0)
}
).ToArray();
I've been looking around and found this answer
But I am not sure what I should use in place of the Projections.Constant being used in that example, and how I should create a group by clause consisting of multiple fields.
It looks like your grouping over multiple columns is correct.
This issue reported in the NHibernate bug tracker is similar: NH-2865 - "Expression type 'NhSumExpression' is not supported by this SelectClauseVisitor."
Problem is that apart from the less-than-helpful error message, it's not really a bug as such. What happens in NH-2865 is that the Sum expression contains something which NHibernate doesn't know how to convert into SQL, which result in this exception being thrown by a later part of the query processing.
So the question is, what does you sum expression contains that NHibernate cannot convert? The thing that jumps to mind is the use of the ternary operator. I believe the NHibernate LINQ provider has support for the ternary operator, but maybe there is something in this particular combination that is problematic.
However, I think your expressions can be written like this instead:
Bet = grp.Sum(a => Math.Min(a.Amount, 0) * -1), // Or Math.Abs() instead of multiplication.
Won = grp.Sum(a => Math.Max(a.Amount, 0))
If that doesn't work, try to use a real simple expression instead, like the following. If that works, we at least know the grouping itself work as expected.
Won = grp.Sum(a => a.Amount)