Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Puzzle

 
Sameer Jamal
Ranch Hand
Posts: 1870
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose x and y are distinct identifiers of type integer. show a simple
context into which either of the code fragments x = 0; y = 1 or y = 1; x= 0 can be substituted so that in most programming languages the resulting substitutions yield different results.
[ February 22, 2006: Message edited by: Sameer Jamal ]
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int x=0;
int y=1;

System.out.println(x-y);
System.out.println(x/y);

- Manish
 
Vishnu Prakash
Ranch Hand
Posts: 1026
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sameer, is the ':' intentional in "x:= 0"? If so, is it intentional that all 3 other expressions do not include a ':'?

I'm thinking maybe the solution is in a for loop, where

is different in effect from

However we need to distinguish between = and == to get this to compile. Most other languages nowadays distinguish between relational operators and assignments - seeing := makes me think of Pascal for example. I don't see a good way to make your text work for any language I'm currently familiar with. Can you confirm the exact syntax, and maybe give a specific languuage it definitely works in?
 
Sameer Jamal
Ranch Hand
Posts: 1870
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Sameer, is the ':' intentional in "x:= 0"? If so, is it intentional that all 3 other expressions do not include a ':'?


No it was not intentional it was just a typing mistake.
There are few languages where comparison operator '==' will not work for eg: VB 6.0

The context can be



In the case that the first code fragment is substituted for the ellipsis, the effect is to set z to 1, while in the second case z gets set to 0.
[ February 22, 2006: Message edited by: Sameer Jamal ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic