Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Make $2 + 2 = 5$

ESCR - not a golfing answer This isn't really an answer to the question, and it's not an attempt at golfing. However, it shows a cute trick for solving this problem that falls out of the way the ...

posted 3mo ago by Olin Lathrop‭  ·  edited 3mo ago by Olin Lathrop‭

Answer
#2: Post edited by user avatar Olin Lathrop‭ · 2024-01-19T14:24:56Z (3 months ago)
  • <h2><a href="http://www.embedinc.com/pic/escr/index.htm">ESCR</a> - not a golfing answer</h2>
  • This isn't really an answer to the question, and it's not an attempt at golfing. However, it shows a cute trick for solving this problem that falls out of the way the ESCR language works.
  • ESCR has no "operators" in the sense of a traditional programming language, only functions. ESCR functions are enclosed in []. The first token within the [] is the function name, which is then followed by whatever parameters are passed to the function. Functions result in a value, which can always be expressed in string format. The entire [...] function reference is replaced by its value.
  • Addition is done with the built-in function "+". For example, [+ 3 4] results in 7.
  • Another wrinkle in ESCR is that new versions of existing symbols can be defined. Each symbol is essentially a stack, with the most recently defined version on top of the stack.
  • New versions of functions can be defined, just like other symbols such as variables. When inside a routine (function, subroutine, or macro), the default version of that routine is the next-older than currently in. Nested routines of the same name therefore call each other down the stack. It is possible to reference a particular version, like the current to do recursion, but requires a deliberate syntax. The default makes it easy to "hook" existing routines.
  • Built-in functions aren't special in this regard. They are seeded into the function symbol table before user code is run, and are implemented with compiled code, but are otherwise indistinguishable from user-defined functions. New versions of these functions can be created, just like with other ESCR symbols.
  • The trick here is to define a new version of the "+" function that returns 5 when both arguments are 2, but the normal sum otherwise. (The actual ESCR "+" function can take any number of arguments, but we'll ignore that). Here is the code that creates a new "+", and demonstrates its use:
  • <pre>
  • function +
  • if [and [= [arg 1] 2] [= [arg 2] 2]]
  • then
  • funcval 5
  • else
  • funcval [+ [arg 1] [arg 2]]
  • endif
  • endfunc
  • show [+ 1 2]
  • show [+ 2 2]
  • show [+ 2 1]
  • </pre>
  • The FUNCTION and ENDFUNC commands start and end the new function definition. Arguments to routines are accessed inside those routines with the ARG function. [arg 1] expands to the first argument, [arg 2] to the second, etc. The IF statement takes the THEN branch when both arguments to the function are 2. In that case, the FUNCVAL command sets the function value to 5 explicitly. Otherwise, the ELSE case is run. Here the function value becomes the sum of the two arguments.
  • Note the use of the "+" function in the ELSE clause to do the addition. The default version referenced inside a routine is the previous version, which in this case is the built-in "+" function.
  • The three SHOW commands write the result of "+" function uses to standard output. The output is:
  • <pre>
  • 3
  • 5
  • 3
  • </pre>
  • <h2><a href="http://www.embedinc.com/pic/escr/index.htm">ESCR</a> - not a golfing answer</h2>
  • This isn't really an answer to the question, and it's not an attempt at golfing. However, it shows a cute trick for solving this problem that falls out of the way the ESCR language works.
  • ESCR has no "operators" in the sense of a traditional programming language, only functions. ESCR functions are enclosed in [ ]. The first token within the [ ] is the function name, which is then followed by whatever parameters are passed to the function. Functions result in a value, which can always be expressed in string format. The entire [...] function reference is replaced by its value.
  • Addition is done with the built-in function "+". For example, [+ 3 4] results in 7.
  • Another wrinkle in ESCR is that new versions of existing symbols can be defined. Each symbol is essentially a stack, with the most recently defined version on top of the stack.
  • New versions of functions can be defined, just like other symbols such as variables. When inside a routine (function, subroutine, or macro), the default version of that routine is the next-older than currently in. Nested routines of the same name therefore call each other down the stack. It is possible to reference a particular version, like the current to do recursion, but requires a deliberate syntax. The default makes it easy to "hook" existing routines.
  • Built-in functions aren't special in this regard. They are seeded into the function symbol table before user code is run, and are implemented with compiled code, but are otherwise indistinguishable from user-defined functions. New versions of these functions can be created, just like with other ESCR symbols.
  • The trick here is to define a new version of the "+" function that returns 5 when both arguments are 2, but the normal sum otherwise. (The actual ESCR "+" function can take any number of arguments, but we'll ignore that). Here is the code that creates a new "+", and demonstrates its use:
  • <pre>
  • function +
  • if [and [= [arg 1] 2] [= [arg 2] 2]]
  • then
  • funcval 5
  • else
  • funcval [+ [arg 1] [arg 2]]
  • endif
  • endfunc
  • show [+ 1 2]
  • show [+ 2 2]
  • show [+ 2 1]
  • </pre>
  • The FUNCTION and ENDFUNC commands start and end the new function definition. Arguments to routines are accessed inside those routines with the ARG function. [arg 1] expands to the first argument, [arg 2] to the second, etc. The IF statement takes the THEN branch when both arguments to the function are 2. In that case, the FUNCVAL command sets the function value to 5 explicitly. Otherwise, the ELSE case is run. Here the function value becomes the sum of the two arguments.
  • Note the use of the "+" function in the ELSE clause to do the addition. The default version referenced inside a routine is the previous version, which in this case is the built-in "+" function.
  • The three SHOW commands write the result of "+" function uses to standard output. The output is:
  • <pre>
  • 3
  • 5
  • 3
  • </pre>
#1: Initial revision by user avatar Olin Lathrop‭ · 2024-01-19T14:23:57Z (3 months ago)
<h2><a href="http://www.embedinc.com/pic/escr/index.htm">ESCR</a> - not a golfing answer</h2>

This isn't really an answer to the question, and it's not an attempt at golfing.  However, it shows a cute trick for solving this problem that falls out of the way the ESCR language works.

ESCR has no "operators" in the sense of a traditional programming language, only functions.  ESCR functions are enclosed in [].  The first token within the [] is the function name, which is then followed by whatever parameters are passed to the function.  Functions result in a value, which can always be expressed in string format.  The entire [...] function reference is replaced by its value.

Addition is done with the built-in function "+".  For example, [+ 3 4] results in 7.

Another wrinkle in ESCR is that new versions of existing symbols can be defined.  Each symbol is essentially a stack, with the most recently defined version on top of the stack.

New versions of functions can be defined, just like other symbols such as variables.  When inside a routine (function, subroutine, or macro), the default version of that routine is the next-older than currently in.  Nested routines of the same name therefore call each other down the stack.  It is possible to reference a particular version, like the current to do recursion, but requires a deliberate syntax.  The default makes it easy to "hook" existing routines.

Built-in functions aren't special in this regard.  They are seeded into the function symbol table before user code is run, and are implemented with compiled code, but are otherwise indistinguishable from user-defined functions.  New versions of these functions can be created, just like with other ESCR symbols.

The trick here is to define a new version of the "+" function that returns 5 when both arguments are 2, but the normal sum otherwise.  (The actual ESCR "+" function can take any number of arguments, but we'll ignore that).  Here is the code that creates a new "+", and demonstrates its use:

<pre>
function +
  if [and [= [arg 1] 2] [= [arg 2] 2]]
    then
      funcval 5
    else
      funcval [+ [arg 1] [arg 2]]
    endif
  endfunc

show [+ 1 2]
show [+ 2 2]
show [+ 2 1]
</pre>

The FUNCTION and ENDFUNC commands start and end the new function definition.  Arguments to routines are accessed inside those routines with the ARG function.  [arg 1] expands to the first argument, [arg 2] to the second, etc.  The IF statement takes the THEN branch when both arguments to the function are 2.  In that case, the FUNCVAL command sets the function value to 5 explicitly.  Otherwise, the ELSE case is run.  Here the function value becomes the sum of the two arguments.

Note the use of the "+" function in the ELSE clause to do the addition.  The default version referenced inside a routine is the previous version, which in this case is the built-in "+" function.

The three SHOW commands write the result of "+" function uses to standard output.  The output is:

<pre>
3
5
3
</pre>