I'm just getting started on F#, and when playing around with operator overloading, I've run into something I don't quite understand. Now, I understand that you can't use, for example, +* as an overloaded prefix operator; it can only be an infix operator. Here's where I get confused, however:
let (+*) a = a + a * a;;
If I run this, fsi tells me that the function (+*) is an int->int. Great, I can dig that -- it's not an overloaded operator, just a normal function named (+*). So, if I do:
printf "%d" ((+*) 6)
I'll get 42, as I expect to. However, if I try:
printf "%d" (+*) 6
or
printf "%d" (+*)6
It won't compile. I can't put the exact error up right now as I don't have access to an F# compiler at this moment, but why is this? What's going on with the binding here?
It's interpreting this:
printf "%d" (+*) 6
Like this:
printf ("%d") (+*) (6)
In other words, passing three curried arguments to printf, the second of which is a reference to the function +*.
Related
I have an EFI Shell tool which uses EDK 1.05 and TCL 8.3 sources. This tool accepts user commands to display PCI-E adapter information and to upgrade firmware on it. I recently ported it to UDK2017. I am using VS2012x86 toolchain to build the tool.
When I run the binary from EFI Shell, TCL reports errors such as these.
can't use invalid octal number as operand of "||"
syntax error in expression "(1<<0)"
syntax error in expression "(0x1<<0)"
I have read about TCL and Octal numbers
Since this issue is not being seen with EDK 1.05 code with the same TCL version, I am wondering if there is any flag I am missing out. I am hoping there is a simple solution to get past this error since there was no change in the TCL version.
Octal Issue
It's hard to be sure, but I suspect with the octal number issue you've got code that's parsing something like 080808 as a number, which is interpreted as octal because of the leading 0 (just like a constant in C or C++) and so can't contain an 8 (or 9). To parse a number definitely as decimal, the scan command is used:
set val 080808
scan $val "%d" parsedVal
# Properly, should check that [scan] has a result of 1, but I probably wouldn't bother
puts "$val -> $parsedVal"
Odd Expression Syntax Error
The other syntax error in expression "(1<<0)" errors are stranger, as those are definitely valid syntax. I've only got versions back to 8.4 on this machine, but…
$ tclsh8.4
% expr (1<<0)
1
The only ways that could be an invalid expression are if it is either in some custom expression language (which would be application-specific; you'll have to read the documentation to figure that out) or if you're using an expression string as a numeric value:
% set val (1<<0)
(1<<0)
% expr {$val + 1}
can't use non-numeric string as operand of "+"
but that wouldn't produce exactly the error you are seeing. Very puzzling indeed!
Use Stack Traces
There is something that might help you figure out what is going on. After an error, the global errorInfo variable has a stack trace generated. For example, after the above erroring expr it has this:
% puts $errorInfo
can't use non-numeric string as operand of "+"
while executing
"expr {$val + 1}"
The good thing is that this tells you exactly what command and where gave you the error; that can make a gigantic difference in your detective work to hunt down your problems.
I can't understand how assignments and use of variables work in Tcl.
Namely:
If I do something like
set a 5
set b 10
and I do
set c [$a + $b]
Following what internet says:
You obtain the results of a command by placing the command in square
brackets ([]). This is the functional equivalent of the back single
quote (`) in sh programming, or using the return value of a function
in C.
So my statement should set c to 15, right?
If yes, what's the difference with
set c [expr $a + $b]
?
If no, what does that statement do?
Tcl's a really strict language at its core; it always follows the rules. For your case, we can therefore analyse it like this:
set c [$a + $b]
That's three words, set (i.e., the standard “write to a variable” command), c, and what we get from evaluating the contents of the brackets in [$a + $b]. That in turn is a script formed by a single command invocation with another three words, the contents of the a variable (5), +, and the contents of the b variable (10). That the values look like numbers is irrelevant: the rules are the same in all cases.
Since you probably haven't got a command called 5, that will give you an error. On the other hand, if you did this beforehand:
proc 5 {x y} {
return "flarblegarble fleek"
}
then your script would “work”, writing some (clearly defined) utter nonsense words into the c variable. If you want to evaluate a somewhat mathematical expression, you use the expr command; that's it's one job in life, to concatenate all its arguments (with a space between them) and evaluate the result as an expression using the documented little expression language that it understands.
You virtually always want to put braces around the expression, FWIW.
There are other ways to make what you wrote do what you expect, but don't do them. They're slow. OTOH, if you are willing to put the + first, you can make stuff go fast with minimum interference:
# Get extra commands available for Lisp-like math...
namespace path ::tcl::mathop
set c [+ $a $b]
If you're not a fan of Lisp-style prefix math, use expr. It's what most Tcl programmers do, after all.
set c [$a + $b]
Running the above command, you will get invalid command name "5" error message.
For mathematical operations, we should rely on expr only as Tcl treats everything as string.
set c [expr $a + $b]
In this case, the value of a and b is passed and addition is performed.
Here, it is always safe and recommended to brace the expressions as,
set c [expr {$a+$b}]
To avoid any possible surprises in the evaluation.
Update 1 :
In Tcl, everything is based on commands. It can a user-defined proc or existing built-in commands such as lindex. Using a bare-word of string will trigger a command call. Similarly, usage of [ and ] will also trigger the same.
In your case, $a replaced with the value of the variable a and since they are enclosed within square brackets, it triggers command call and since there is no command with the name 5, you are getting the error.
I would like to apply uncrustify (via beautify in the Atom editor and a config file) to CUDA code. However, I don't know how to tell uncrustify to recognize CUDA kernel calls which have the following structure:
kernelName <<<N,M>>> (arg0,arg1,...);
However, uncrustify has problems with the <<< >>> and applying it gives the following unpleasant result
kernelName << < N, M >> >
(arg0,arg1,...);
I would like to have it look more like a function call and also avoid the formatting of <<< to << <. Ideally, the result would look like
kernelName <<< N, M >>> (arg0,arg1,
...); // line break if argument list is too long
Which arguments can I add to my config.cfg to achieve the above result?
Thank you very much.
Looking through whole documentation of uncrustify, I have found 2 arguments that could influence in your CUDA kernel style:
sp_compare { Ignore, Add, Remove, Force }
Add or remove space around compare operator '<', '>', '==', etc
And:
align_left_shift { False, True }
Align lines that start with '<<' with previous '<<'. Default=true
You can try to play around with these parameters to be more closely to the solution although I would try something like:
sp_compare = Remove
align_left_shift = False
Good morning fellow programmers!
i'm working on a project using haskell, and i've wanted to know how to run a haskell function without having to type two lines on ghci, for example
ghci filename.hs function
This can only be done doing:
ghci filename.hs
function
????
I'm looking for something like the main () in C,which runs automatically when you compile the program
Is there something like that?
I've been checking the -e option on ghci, but i cant seem to get it to work!
Thank you very much!
Cheers!
You're probably looking for ghc -e instead:
> echo 'foo x y z = x+y*z' > foo.hs % let's make a foo.hs file
> ghc foo.hs -e 'foo 1 2 3' % call the function in foo.hs
=> 7
Also, note that you can also use the :reload command in ghci. Load the file in ghci, edit, type :reload and test again. Also, if this seems too tedious, you can also define a ghci macro which allows you to reload and test your function at the same time:
> :def test \x -> return (":reload\n" ++ x)
> :test foo 1 2 3
=> Ok, modules loaded: Foo.
7
If you're looking to build real programs instead of quickly testing
your functions, then you'd better read the other answers on writing
main functions.
I assume function has the type IO (). Then you can just let main = function, and use for example runhaskell modulename from the command line. As in C, main is a special function.
To clarify a bit, just in case: If your function is a pure one, i.e. one whose type does not invovle IO, you can't really "run it". I guess it's a simplification to say this, but essentially what GHCi does is to call print function. If you want to mimic this, you can use something like main = print function and use runhaskell. This assumes function's type is an instance of Show.
I have to run TCL code written for version 8.5 with the interpreter of version 8.3.
I am having problems with eq. Seems 8.3 doesn't recognize it. A simple code:
% expr { "a" eq "b" }
returns an error message like:
syntax error in expression "a eq b"
I am trying to fix this by adding an interp alias before everything like this:
interp alias {} eq {} ==
but seems it has no effect.
Is there any way to make eq an alias for ==? If no, is the only way to get rid of this error is to replace all eq statements by == statements?
The eq and ne operators were introduced in Tcl 8.4. See Changes in Tcl/Tk 8.4 on the Tcler's wiki. There is no way to make "eq" be an alias for "==" within an expression. Your attempt to use the alias command only resulted in creating a command named eq that does whatever the command == does (specifically, fail unless you actually have a command named ==).
If you absolutely must find a way to make this work, in your 8.3 interpreter you can rename expr to be something else (eg: _expr), then create your own expr command that does a string substitution before calling the renamed expr command. This is fraught with peril since you have to make sure to only substitute operators and not the data being compared.
I'm not even sure this code works properly in tcl 8.5
First make sure your expr is between curly brackets:
% expr {"a" eq "b"}
The alias allows you to create an alias for a command, but eq is not a command, it´s a subcommand of the expr command, so you would really need to create your own proc expr args wrapper.
Why do you need to use tcl 8.3? it's a really old version.
Tcl 8.3 is rather old, and the recommended path for upgrading from TclPro (as I see you're using from comments) to something less… antediluvian… is to purchase the Tcl Dev Kit from ActiveState, which is at least the spiritual successor and has current support. Nobody supports TclPro any more.
That said…
Now, with Tcl 8.3 the equivalent of the functionality of the eq operator was the string equal command (in 8.4, they actually compile to the same bytecode; one is a short-cut for the other). This means that for this:
if {"a" eq "b"} {...
would be instead written as this:
if {[string equal "a" "b"]} {...
though if you've definitely got a non-integer for one of the arguments, you can just use this:
if {"a" == "b"} {...
There is no mechanism for aliasing operators in any version of Tcl (well, certainly not up to 8.6, which is the current development/beta version).
The Gripping Hand
As Bryan says, you can write your own versions of expr (and if and for and while) that have this extended functionality, but that's a lot of work (i.e., months including all the testing, though you can probably reuse the test suite from Tcl 8.4 to help) and it's just so you can use a version of Tcl that is known to be obsolete. (I have no idea if it has security issues in it, and you can't pay me enough for me to want to try to find out.) You're better off paying for the TDK.