expr is giving unexpected results - tcl

expr is giving unexpected results for 4 characters (t, n, f, y). And if you are doing some further calculation. then code is breaking. I could not understand why this is happening?
% expr (F)
F
% expr (F)*1
can't use non-numeric string as operand of "*"
And,
% expr (t)
t
% expr (n)
n
% expr (f)
f
% expr (y)
y
This is coming file for charcters : t, n, f, y. There are no variables named by these characters. It should flag variable not found or some other valid error. Am i missing some thing?

The [expr] conditions of commands such as [if] and [while] expect the expression to evaluate to a boolean, i.e., an integer or one of the following string values:
true, on, yes
false, off, no
I believe t, y, f and n are shortcuts for these.

I think you are expecting something wrong from expr.
That command is intended for evaluating expressions. It can do arithmetical operations on number, compare strings or number, execute some mathematical functions, and such.
Your lines
% expr (F)
% expr (t)
% expr (n)
% expr (f)
% expr (y)
all do the same thing: they ask to perform no operation on a literal string with higher precedence (the braces). So? There is nothing more and expr returns the string itself.
In
% expr (F)*1
however, you are trying to multiply a string to a number: an operation which is not defined. Indeed, expr gives you an error saying that one of the operands of * is a non numeric string (which number F should represent?).
With a literal string such F, or y, you can ask string comparison. For example, you can do these:
% expr F < f
1
(because in my encoding the upper case letters come before lower case ones)
% expr F == y
0
and so on.
So, expr is not giving any unexpected result, but maybe your expectations are wrong.

Related

Function of function(functional) in Maxima

I want to define a function of a function ( maybe functional) in Maxima. For example, I want to define T in the code:
f(r,GN,M)::=(2*GN*M^2)*(2*GN*M/r-(2*GN*M/(2*r))^2);
X(r,GN,M) ::= 2*GN*M/(2*r)+2*GN*M/r*(1-2*GN*M/(4*r));
T(r,GN,M) ::= diff(f(r,GN,M),r)+X(r,GN,M);
But I don't know the code.
In maxima it is convenient to use expressions.
Define two expressions
(%i1) display2d: false $
(%i2) f: (2*GN*M^2)*(2*GN*M/r-(2*GN*M/(2*r))^2) $
(%i3) X: 2*GN*M/(2*r)+2*GN*M/r*(1-2*GN*M/(4*r)) $
Define a function which operates on two expressions
(%i4) T(f, X) := diff(f, r) + X $
Call it. Result is an expression
(%i5) dfpX: T(f, X);
(%o5) (2*GN*M*(1-(GN*M)/(2*r)))/r+(GN*M)/r+2*GN*M^2
*((2*GN^2*M^2)/r^3-(2*GN*M)/r^2)
You can create a function based on dfpX
(%i6) define(f(r, GN, M), dfpX);
(%o6) f(r,GN,M):=(2*GN*M*(1-(GN*M)/(2*r)))/r+(GN*M)/r
+2*GN*M^2*((2*GN^2*M^2)/r^3-(2*GN*M)/r^2)
and call it
(%i7) f(1, 2, 3);
(%o7) 2142
but you can stay with expressions
(%i8) subst([r=1, GN=2, M=3], dfpX);
(%o8) 2142
Define T as a function, rather than a macro, so that it evaluates its arguments.
(%i) T(r,GN,M) := diff(f(r,GN,M),r)+X(r,GN,M)$
Now define a function t on T which quotes ' its arguments (so that T knows which variable to differentiate with respect to) and then evaluates the result %% with ev.
(%i) t(r,GN,M) := (T('r,'GN,'M),ev(%%))$
(%i) t(1,2,3);
(%o) 2142

OCaml argument names

From a reading:
Labels for arguments are often the same as the variable names for
them. OCaml provides a shorthand for this case. The following are
equivalent:
let f ~name1:name2 ~name2:name2 = name1+name2
let f ~name1 ~name2 = name1 + name2
When I put in the first line I get this error
Error: Unbound value name1
Hint: Did you mean name2?
I don't understand how they're equivalent if the first line isn't a valid function.
The syntax of function arguments allows different expressions.
Given an expression expr that evaluates to a function f, the following syntax are allowed with the following rules:
expr arg (given an expression arg): evaluate arg to a value v; the result is the value of f evaluated with argument v
expr ~lbl (given a value named lbl): only when f has type lbl:a -> b for some types a and b, the result is the value of f ~lbl evaluated with argument a; OR, when f has type ?lbl:a -> b (and then, lbl must have type a for typechecking).
expr ~lbl:arg (given an expression arg): this is the same thing as let lbl = arg in exp ~lbl (except that the evaluation order of expr and arg is undefined).
expr ?lbl (given a value named lbl): only when f has type ?lbl:a -> b for some types a and b, the result is the value of f ~lbl evaluated with argument a; lbl must have type a option for typechecking. This appears mostly when forwarding optional arguments from one function to another.
expr ?lbl:arg (given an expression arg): this is the same thing as let lbl = arg in exp ?lbl (except that the evaluation order of expr and arg is undefined).

How to force expr to address a value as a string and not a number?

When TCL gets a string that starts with a 0 as its return value, it'll treat it as an octal number and will return the decimal value of the octal number. Is there a way to circumvent it and force expr to address the value as a string?
I encounter this problem because I have a line:
set val [expr {( $obj == "" ) ? "" : [$obj data]}]
And one the results of the [$obj data] operation is a binary string starting with 0, and the expr turns it into another number. Is there a way to fix this without turning the expr into an if?
The expr command is defined to convert its result to a number if it is legal to do so. It's been this way sinceā€¦ well, since at least Tcl 7.0 and probably since the first version of Tcl to have an expr command (which takes it a hugely long way back). This means that if you return a valid octal number (which 09 isn't), expr will convert it.
If this behaviour isn't desired, don't use expr for conditionals; use if. In your case, this works quite nicely (and I think it's clearer this time with the then and else pseudo-keywords).
set val [if {$obj == ""} then {} else {$obj data}]
(At the bytecode level, this generates almost the identical bytecode to what your original does, except it omits a call to the tryCvtToNumeric operation; that's the one you say you don't want!)
[warning: not an answer but a coment requiring formatting]
I'm not observing the same results
% proc obj {args} {return "09"} ;#specifically using invalid octal value
% set obj obj
obj
% $obj data
09
% set val [expr {($obj == "") ? "" : [$obj data]}]
09
% info patchlevel
8.6.1
What Tcl version are you using?

Haskell parse error on input '='

This is a Quicksort function. But I've got an error at 5:46
--sort function
quicksort [] = []
quicksort (x:xs) = (quicksort lesser) ++[x] ++ (quicksort greater)
where lesser = filter (<) xs
greater = filter (>=) xs
What's the problem?
It seems that the function is correct.
It appears you have a simple whitespace error.... lesser and greater need to be indented equally, so that they begin on the same column.

OCaml: Using a comparison operator passed into a function

I'm an OCaml noob. I'm trying to figure out how to handle a comparison operator that's passed into a function.
My function just tries to pass in a comparison operator (=, <, >, etc.) and an int.
let myFunction comparison x =
if (x (comparison) 10) then
10
else
x;;
I was hoping that this code would evaluate to (if a "=" were passed in):
if (x = 10) then
10
else
x;;
However, this is not working. In particular, it thinks that x is a bool, as evidenced by this error message:
This expression has type 'a -> int -> bool
but an expression was expected of type int
How can I do what I'm trying to do?
On a side question, how could I have figured this out on my own so I don't have to rely on outside help from a forum? What good resources are available?
Comparison operators like < and = are secretly two-parameter (binary) functions. To pass them as a parameter, you use the (<) notation. To use that parameter inside your function, you just treat it as function name:
let myFunction comp x =
if comp x 10 then
10
else
x;;
printf "%d" (myFunction (<) 5);; (* prints 10 *)
OCaml allows you to treat infix operators as identifiers by enclosing them in parentheses. This works not only for existing operators but for new ones that you want to define. They can appear as function names or even as parameters. They have to consist of symbol characters, and are given the precedence associated with their first character. So if you really wanted to, you could use infix notation for the comparison parameter of myFunction:
Objective Caml version 3.12.0
# let myFunction (#) x =
x # 10;;
val myFunction : ('a -> int -> 'b) -> 'a -> 'b = <fun>
# myFunction (<) 5;;
- : bool = true
# myFunction (<) 11;;
- : bool = false
# myFunction (=) 10;;
- : bool = true
# myFunction (+) 14;;
- : int = 24
#
(It's not clear this makes myFunction any easier to read. I think definition of new infix operators should be done sparingly.)
To answer your side question, lots of OCaml resources are listed on this other StackOverflow page:
https://stackoverflow.com/questions/2073436/ocaml-resources
Several possibilities:
Use a new definition to redefine your comparison operator:
let myFunction comparison x =
let (#) x y = comparison x y in
if (x # 10) then
10
else
x;;
You could also pass the # directly without the extra definition.
As another solution you can use some helper functions to define what you need:
let (/*) x f = f x
let (*/) f x = f x
let myFunction comparison x =
if x /* comparison */ 10 then
10
else
x