Why does whitespace in my F# code cause errors? - syntax

I've been tinkering with the F# Interactive.
I keep getting weird results, but here's one I can't explain:
The following code returns 66, which is the value I expect.
> let f x = 2*x*x-5*x+3;;
> f 7;;
The following code throws a syntax error:
> let f x = 2*x*x - 5*x +3;;
stdin(33,21): error FS0003: This value is not a function and cannot be applied
As you can see, the only difference is that there are some spaces between the symbols in the second example.
Why does the first code example work while the second one results in a syntax error?

The problem here is the use of +3. When dealing with a +/- prefix on a number expression white space is significant
x+3: x plus 3
x +3: syntax error: x followed by the positive value 3
I've run into this several times myself (most often with -). It's a bit frustrating at first but eventually you learn to spot it.
It's not a feature without meaning though. It's necessary to allow application of negative values to functions
myFunc x -3: call function myFunc with parameters x and -3

The error message says that you are trying to call a function x with the argument +3 ( unary + on 3) and since x is not a function, hence the This value is not a function and cannot be applied

Related

Is there a way to refer to the result of the previous command in PSCi?

Coming from Haskell, I'm accustomed to using it to refer to the result of the previous command in GHCi:
ghci> 1+1
2
ghci> :type it
it :: Num a => a
ghci> it*2
4
I went looking to see if there was something similar in the PureScript REPL, but I couldn't find anything about it on the "Differences from Haskell" page in the PureScript docs (which makes a certain kind of sense now that I've had some time to think about it, since it is a specific feature of GHCi, not of the Haskell language itself), or in the PSCi documentation. In GHCi I can :show bindings to discover that it has been bound, but PSCi's :show command doesn't accept the bindings argument, and from the built-in PSCi help :? it's not obvious to me if there's a different command that does the same thing. (I know PSCI's :browse lets you examine the functions from a specific module, so since errors reported in PSCi often begin with Error found: in module $PSCI I tried :browse $PSCI, but this produces an indentation error.)
Some errors I've encountered seem to suggest that PSCi is trying to bind the result of a computation to the name it (I've cut some whitespace but otherwise this is copied verbatim from a fresh spago repl session in a toy project—note the final line, in value declaration it):
> import Prelude
> 1+'a'
Error found:
in module $PSCI
at :1:3 - 1:6 (line 1, column 3 - line 1, column 6)
Could not match type
Char
with type
Int
while checking that type Char
is at least as general as type Int
while checking that expression 'a'
has type Int
in value declaration it
But after a successful computation, it isn't available:
> 1+1
2
> it
Error found:
at <internal>:0:0 - 0:0 (line 0, column 0 - line 0, column 0)
The value of it is undefined here, so this reference is not allowed.
This is a CycleInDeclaration error, which is different from the UnknownName error produced when trying to use some other undefined name like x or foo. This all makes me suspect there is something special going on with it and I'm just missing something obvious about how to make it available for use in the REPL, but as someone brand new to PureScript, I'm not sure where to look next.

Error_Generator expression_spyder(python 3.6)

I develop mathematical model using gurobi solver, in python.
I get the following error while running:
SyntaxError: Generator expression must be parenthesized if not sole argument
My constraint is:
My code is:
for s in S:
m.addConstr(sum(x[s,s0,c,i] for s0 in S0 for c in C for i in D,s!=p) == 1,'C_3')
First of all, everything comes whenever you add that comma: ,s!=p.
I just emulated your code with a model I'm working on, and I obviously got the same error. Look around (e.g. if else in a list comprehension), and you will see that the only mistake you had was that the iterator within the generator wasn't well specified. That means, you had to use an if clause in order to achieve what you wanted:
for s in S:
m.addConstr(
quicksum(x[s,s0,c,i] for s0 in S0 for c in C for i in D if s!=p) == 1,
'C_3_'+str(s) )
By the way, as included in the code, you should use quicksum instead of sum. Furthermore, I would suggest to try changing the order of the iterators; in other words, it's not the same for a computer to enumerate a list of 5 elements 1000 times than to enumerate 5 times a list of 1000 elements, and this is something quite important in Python timing.
As a side note, I got into this question while looking for this:
TypeError: unsupported operand type(s) for +: 'generator' and 'generator'

ocaml: Basic syntax for function of several arguments

I am learning OCaml and I'm a complete beginner at this point. I'm trying to get used to the syntax and I just spent 15 minutes debugging a stupid syntax error.
let foo a b = "bar";;
let biz = foo 2. -1.;;
I was getting an error This expression has type 'a -> string but an expression was expected of type int. I resolved the error, but it prompted me to learn what is the best way to handle this syntax peculiarity.
Basically OCaml treats what I intended as the numeric constant -1. as two separate tokens: - and 1. and I end up passing just 1 argument to foo. In other languages I'm familiar with this doesn't happen because arguments are separated with a comma (or in Scheme there are parentheses).
What is the usual way to handle this syntax peculiarity in OCaml? Is it surrounding the number with parentheses (foo 2. (-1.)) or there is some other way?
There is an unary minus operator ~-. that can be used to avoid this issue: foo ~-.1. (and its integer counterpart ~-) but it is generally simpler to add parentheses around the problematic expression.

compiling issue with atom and f#

hey guys I'm trying to compile this function called 2v2 that multiplies any number times 2. I'm doing it using Atom with ionide but when i run it using the f# interactive, it gives me this error saying that it doesn't see the rest of the function. Any suggestions?
let times 2v2 n =
let p = 2 in
n*2;;
times 2v2 10;;
let times 2v2 n =;;
let times 2v2 n =;;
----------^^^
/stdin(4,11): error FS1156: This is not a valid numeric literal. Sample formats include 4, 0x4, 0b0100, 4L, 4UL, 4u, 4s, 4us, 4y, 4uy, 4.0, 4.0f, 4I.
If I understand your question correctly, you want to have a function called times 2v2 that multiplies its parameter with 2. Since the name of your function contains a space, you have to enclose it with double backticks. For example:
let ``times 2v2`` n = 2 * n
And also use these when calling the function:
``times 2v2`` 10
Without double backticks, identifiers must not begin with numbers or contain spaces. Otherwise, the compiler would have a hard time figuring out whether it's dealing with one or multiple identifiers.
A minimalist way to make the question's code valid would be to remove the space between times and 2v2. The variable p would serve no purpose though.

How do i count words in prolog?

I try to count words in a string in prolog. Like "No, I am definitely not a pie!"
To give me the number 7 and next example "w0w such t3xt... to give number 5.
I had thougt about subtract that are a library function and only get back white-characters. But the problem then is No way will give back 5 and not two words.
I thought about
filter([],L).
filter([X,Y|XS],[Y|XS]):- X = ' ',Y = ' ',L = [Y|XS], filter([Y|XS],L).
filter([X|XS],L):- filter(Xs,L).
That will remove white spaces and get back No way but it dosent work anbody have a tip.
Strings in Prolog are lists of character codes not of atoms, what explains why tests like X=' ' fail. See what is the result of executing
write("ab cd"), nl.
in your Prolog system.
You have errors in your 3 clauses:
What to do you expect the first clause to return in the last argument?
L is, as any other variable in a Prolog program, a variable that is local to the clause it appears in, never a global variable.
The second clause unifies L with a list and you use it as second argument of the recursive call: do you expect the recursive call to change the value of L? This will never be the case: in Prolog there is no assignment of variables, changes are made by building terms and unifying them with new variables.
What happens to X in your third clause???

Resources