I keep getting
'Block following this 'let' is unfinished. Expect an expression'
when trying to compute a function.
Here is my code:
let colourTheCountries (chart: Chart) =
Set.fold(extColouring chart) Set.empty Set.empty
How do I terminate the expression?
I want the value of the Set.fold to be the result.
After looking around on the internet I couldn't figure out why I was getting this error. I notice a function that was previously working below this one was also getting an error.
Turns out the functions above was indented by an extra space.
Related
Hello can someone please explain me how can you deal with failed computations (in our case parsings) in Haskell when performed in a list,retrieving the successful elements?
The error i get is
main: Prelude.read: no parse and that stops all the list from being processed
I am using a forM over a collection of Text , and for each element i am using a read::String->Double for the result value.
Currently the parsing fails at the first element and i can not parse the remaining elements.How can i make single elements "fail-able" but still get partial results ( for the elements of the list that could be parsed) ?
Example :Input: ["asta","1.23","2.44"]
Desired Output:[1.23,2.44]
import qualified Data.Text as T
parseDfile::[T.Text]->IO [Maybe Double]
parseDfile []=do
return [Nothing]
parseDfile lines = forM lines $ \line ->
do
Prelude.putStrLn ("line value:"++(T.unpack line))
let value = (read::String->Double) . T.unpack $ //fails here for first element
print .show $ value
return (Just value)
P.SDo i have to define a method using the Maybe monad separately only for that one line of code ?
The Text.Read library also has a function called readMaybe that returns a Maybe a instead of just an a like read does.
In the case that you're not sure whether or not a string can be parsed, you clearly want a Maybe a. Now you need to deal with the Maybe though, however the Maybe monad has tons of functions that do exactly what you need.
For more complicated parsing you could look into the Haskell ParseLib which is really good. However it might be a little overkill if you're not trying to parse more than your example.
I just tested my code to build the dot diagram, when I tried to union two relations together and passed the the sumed up relation, following error is reported:
Expected rel[loc,loc] (...), but got rel[loc,loc]
I am not sure what relloc, loc means because each of the separated relations works correctly. Could please you tell me why?
Yes, that message can be confusing!
rel[loc,loc] (...) means a type of function which returns a rel[loc,loc] given some arguments ... which are unspecified in this message
rel[loc,loc] is just the type of a binary relation of loc
We sometimes see this kind of message occurring when ( ) brackets are used accidentally instead of [ ] for indexing into a relation. If you provide a little more context code, maybe we can diagnose the issue from that more precisely.
4> abs(1).
1
5> X = abs.
abs
6> X(1).
** exception error: bad function abs
7> erlang:X(1).
1
8>
Is there any particular reason why I have to use the module name when I invoke a function with a variable? This isn't going to work for me because, well, for one thing it is just way too much syntactic garbage and makes my eyes bleed. For another thing, I plan on invoking functions out of a list, something like (off the top of my head):
[X(1) || X <- [abs, f1, f2, f3...]].
Attempting to tack on various module names here is going to make the verbosity go through the roof, when the whole point of what I am doing is to reduce verbosity.
EDIT: Look here: http://www.erlangpatterns.org/chain.html The guy has made some pipe-forward function. He is invoking functions the same way I want to above, but his code doesn't work when I try to use it. But from what I know, the guy is an experienced Erlang programmer - I saw him give some keynote or whatever at a conference (well I saw it online).
Did this kind of thing used to work but not anymore? Surely there is a way I can do what I want - invoke these functions without all the verbosity and boilerplate.
EDIT: If I am reading the documentation right, it seems to imply that my example at the top should work (section 8.6) http://erlang.org/doc/reference_manual/expressions.html
I know abs is an atom, not a function. [...] Why does it work when the module name is used?
The documentation explains that (slightly reorganized):
ExprM:ExprF(Expr1,...,ExprN)
each of ExprM and ExprF must be an atom or an expression that
evaluates to an atom. The function is said to be called by using the
fully qualified function name.
ExprF(Expr1,...,ExprN)
ExprF
must be an atom or evaluate to a fun.
If ExprF is an atom the function is said to be called by using the implicitly qualified function name.
When using fully qualified function names, Erlang expects atoms or expression that evaluates to atoms. In other words, you have to bind X to an atom: X = atom. That's exactly what you provide.
But in the second form, Erlang expects either an atom or an expression that evaluates to a function. Notice that last word. In other words, if you do not use fully qualified function name, you have to bind X to a function: X = fun module:function/arity.
In the expression X=abs, abs is not a function but an atom. If you want thus to define a function,you can do so:
D = fun erlang:abs/1.
or so:
X = fun(X)->abs(X) end.
Try:
X = fun(Number) -> abs(Number) end.
Updated:
After looking at the discussion more, it seems like you're wanting to apply multiple functions to some input.
There are two projects that I haven't used personally, but I've starred on Github that may be what you're looking for.
Both of these projects use parse transforms:
fun_chain https://github.com/sasa1977/fun_chain
pipeline https://github.com/stolen/pipeline
Pipeline is unique because it uses a special syntax:
Result = [fun1, mod2:fun2, fun3] (Arg1, Arg2).
Of course, it could also be possible to write your own function to do this using a list of {module, function} tuples and applying the function to the previous output until you get the result.
I am new in Eralng . get a little query about applying functions
assumming got a funciton defined :
mysum(X) -> fun(Y)-> X + Y end.
then try to calling like this
mysum(32)(332)
getting error
* 1: syntax error before: '('
so I had to
apply(mysum(32),[333])
or
M = mysum(32), M(333)
but I would like to know a little bit more , why it is not supporting , what is the disadvantage
As you expected, mysum return a function. you must enclose the evaluation inside parenthesis to satisfy the erlang parser:
(mysum(32))(332)
this spelling is obviously not ambiguous.
Your expression seems not ambiguous because you know that mysum(32) is a function, but the types are solved at run time in erlang, so the parser has no idea of what is mysum(32), it is expecting some help here to know what it has to do: the parenthesis, the apply or the intermediate variables, but it could be an operator or a separator.
I'm trying to program a constraints file for a XC2C256-7VQ100, hence I used a line like follows:
NET "led_1" LOC=7;
However with the above I get an error during translate saying:
Error: Constraint 'LOC' has a value '7' which is invalid. Use the following:
Text that matches the regular expression: *i!:^(soft)|(hard)|(level)$
However I cannot find anywhere information on what soft/hard/level means and/or why I can't just use the pin number specified in the XC2C256 data sheet. Could someone shed some light on this for me?
Thanks.
The correct syntax for LOC is:
NET "LED_1" LOC = "7";
Try this, and I am sure your problem will be solved.