Questions tagged [expression-evaluation]

Anything related to expression evaluation, i.e. the process of determining the value of an expression in running code.

Anything related to expression evaluation, i.e. the process of determining the value of an expression in running code.

189 questions
31
votes
1 answer

Idris eager evaluation

In Haskell, I might implement if like this: if' True x y = x if' False x y = y spin 0 = () spin n = spin (n - 1) This behaves how I expect: haskell> if' True (spin 1000000) () -- takes a moment haskell> if' False (spin 1000000) () --…
Snowball
  • 11,102
  • 3
  • 34
  • 51
29
votes
4 answers

C++ and PHP vs C# and Java - unequal results

I found something a little strange in C# and Java. Let's look at this C++ code: #include using namespace std; class Simple { public: static int f() { X = X + 10; return 1; } static int X; }; int Simple::X…
Dmitry K.
  • 3,065
  • 2
  • 21
  • 32
21
votes
14 answers

Evaluate dice rolling notation strings

Rules Write a function that accepts string as a parameter, returning evaluated value of expression in dice notation, including addition and multiplication. To clear the things up, here comes EBNF definition of legal expressions: roll ::= [positive…
samuil
  • 5,001
  • 1
  • 37
  • 44
20
votes
6 answers

Why is "a^=b^=a^=b;" different from "a^=b; b^=a; a^=b;"?

I tried some code to swap two integers in Java without using a 3rd variable, using XOR. Here are the two swap functions I tried: package lang.numeric; public class SwapVarsDemo { public static void main(String[] args) { int a = 2984; …
Abhishek Oza
  • 3,340
  • 1
  • 27
  • 35
15
votes
1 answer

Does Java strictfp modifier have any effect on modern CPUs?

I know the meaning of the strictfp modifier on methods (and on classes), according to the JLS: JLS 8.4.3.5, strictfp methods: The effect of the strictfp modifier is to make all float or double expressions within the method body be explicitly…
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
13
votes
3 answers

Double assignment of the same variable in one expression in C++11

The C++11 standard (5.17, expr.ass) states that In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. With respect to an …
12
votes
2 answers

How does this let expression work?

Given this line of Haskell code, my task was to evaluate it to its most simple form. let g h k = (\x -> k (h x)) in g (+1) (\x -> x+x) 20 I have already been given the answer (and of course evaluated it myself in GHCI): 42 However, I would like…
11
votes
4 answers

Execution order of f1() + f2()*f3() expression and operator precedence in JLS

Given an expression f1() + f2()*f3() with 3 method calls, java evaluates (operands of) addition operation first: int result = f1() + f2()*f3(); f1 working f2 working f3 working I (wrongly) expected f2() to be called first, then f3(), and finally…
11
votes
6 answers

Passing non final objects to method references

What is the explanation that s.get() returns "ONE" the second time as well? String x = "one"; Supplier s = x::toUpperCase; System.out.println("s.get() = " + s.get()); x = "two"; System.out.println("s.get() = " + s.get()); Update: Compare it…
10
votes
6 answers

Recursive expression evaluator using Java

I am going to write an expression evaluator which only does addition and subtraction. I have a simple algorithm to do that; but, I have some implementation problems. I considered an expression as (it is a String) "("
629
  • 308
  • 1
  • 6
  • 15
10
votes
2 answers

R: get names of arguments passed in `...`

In a simple function using ... to supply a list of arguments, is it possible for that function to find the names of the objects passed from the calling environment? If so, how? This arises in the context of the question, printing matrices and…
user101089
  • 3,756
  • 1
  • 26
  • 53
9
votes
2 answers

Can you evaluate a string in Swift?

I have a variable and I have a function stored in it as a string: var x = "func myFunction(y :Int) {println(y)}" Is there a way to evaluate the string and run the function?
Krncy
  • 93
  • 1
  • 3
8
votes
3 answers

Macro which prints an expression and evaluates it (with __STRING)

For learning and demonstrating, I need a macro which prints its parameter and evaluates it. I suspect it is a very common case, may be even a FAQ but I cannot find actual references. My current code is: #define PRINT(expr) (fprintf(stdout, "%s ->…
bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
8
votes
1 answer

Describing operator precedence using EBNF

I have written tokenizer and expression evaluator for a preprocessor language that I plan to use in my later projects. I started thinking that maybe I should describe the language with EBNF (Extended Backus–Naur Form) to keep the syntax more…
8
votes
2 answers

updating references in an expression with a nested assignment

Looking at this example code similar to this question: public class A { public static void main(String args[]){ A a = new A(); System.out.println(a.equals((a = null))); } } This prints false. Why doesn't it fail with a…
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
1
2 3
12 13