Post History
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 ...
Answer
#2: Post edited
- <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
<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>