What are usecase add1 and sub1?

In Racket, there are two Generic Number procedures called add1 and sub1 , which is equivalent to (curry + 1) and (curryr - 1) respectively.

Are these procedures for stylistic use or do they have some kind of optimization benefit? What is the history of these procedures?

+5
source share
2 answers

History?

We need to get back to the time until 1986, when the revised revised report on the scheme was discussed.

Dan Friedman wrote:

When describing arithmetic, I find the characters "1+" and "-1+" very harmful. The + function is indicated by the β€œ+” symbol, and this confuses people who are trying to understand the primitive recursive definition of +. For purely pedagogical reasons, I would like to see "add1" and "sub1" are optional. I know that macscheme and PC-Scheme have enabled them. I do not like several names for the same design, but there are several examples in the report.

More details here:

https://groups.csail.mit.edu/mac/ftpdir/scheme-mail/HTML/rrrs-1986/msg00246.html https://groups.csail.mit.edu/mac/ftpdir/scheme-mail/HTML /rrrs-1986/msg00251.html

It turns out that the name add1 did not fall into the minimum standard, but some (not minimal) implementations included it (for example, Chez Scheme).

According to the practical scheme [1], the Chez Scheme, Racket, and Chicken Scheme add1 include add1 as a standard. I am sure there are other implementations:

[1] http://practical-scheme.net/wiliki/schemexref.cgi?add1

Regarding its use: it is more convenient (and easier to read)

 (map add1 xs) 

than

 (map (lambda (x) (+ x 1)) xs) 

There shouldn't be any difference in performance these days (but don't take my word on it - make a benchmark). Once upon a time, a simple compiler could create better code for the first expression. A decent compiler needs the same output for both expressions.

+3
source

According to Racket Docs, this is a syntax suger :

Increment (Appendix 1) and decrease (Subtraction 1) are somewhat common in programming. Using names for them is intended to make it easier for someone to read the code in order to immediately see which of the two operations is intended.

Performance: Using the built-in '+' is the best way to improve performance. ' General arithmetic operations ' (+, -, <,>, etc.) are built in by the JIT racket docs compiler :

The nested fixnum and flonum arithmetic operations are some of the most important advantages of the JIT compiler. For example, when + is applied to two arguments, the generated machine code checks to see if the two arguments are fixnums, and if so, it uses the machine instruction to add numbers (and check for overflow).

The performance difference is β€œso small that it’s hard to detect,” from the JIT compiler schema :

The JIT compiler works gradually as functions are applied, but the JIT compiler uses only limited use of runtime information when compiling procedures, because the code for a given module body or lambda abstraction is compiled only once. Glossality of JIT compilation is a single procedure body, not counting the body of any lexically nested procedures. The overhead for compiling JIT is usually so small that it is difficult to detect.

Some of the disadvantages of adding a definition to a global environment are : memory usage, namespace conflicts, increased GE service.

Some of the benefits of adding a definition to a global environment are : readability, ease of maintenance, reusable code.

+2
source

Source: https://habr.com/ru/post/1274940/


All Articles