Literal identifier for scheme custom syntax structure

Source: Internet
Author: User

The scheme language allows you to use let-syntax and other mechanisms to customize special Syntax structures. The pattern language used to define this syntax structure has an inconspicuous literal identifier, which is the <literals> In the following syntax:

(Syntax-rules <literals> <syntax rule> ...)

The interpretation of literal identifier in scheme standard r5rs is obscure. I wrote a small example based on my own understanding, which can play a complementary role:

; Define the syntax Z when literal identifier is not used
(Define-syntax Z
(Syntax-rules ()
(Z (+ x y ))))
; The result of the following row is 7
(Z (+ 3 4 ))
; The result of the following row is-1, which plays a role in the plus sign after the local change.
(Let (+-) (Z (+ 3 4 )))

; Use literal identifier to define the syntax Z
(Define-syntax Z
(Syntax-rules (+)
(Z (+ x y ))))
; The result of the following row is still 7
(Z (+ 3 4 ))
; The following line returns an error: Z: bad syntax in: (Z (+ 3 4 ))
; The plus signs with different meanings (actually different binding) cannot be applied to the syntax.
(Let (+-) (Z (+ 3 4 )))

With the above example, the complicated example mentioned in r5rs is better understood:

(Define-syntax cond
(Syntax-rules (else =>)
(Cond (else result1 result2 ...))
(Begin result1 result2 ...))
(Cond (test => result ))
(Let (temp test ))
(If temp (result temp ))))
(Cond (test => result) clause1 clause2 ...)
(Let (temp test ))
(If temp
(Result temp)
(Cond clause1 clause2 ...))))
(Cond (TEST) test)
(Cond (TEST) clause1 clause2 ...)
(Let (temp test ))
(If temp
Temp
(Cond clause1 clause2 ...))))
(Cond (test result1 result2 ...))
(If test (begin result1 result2 ...)))
(Cond (test result1 result2 ...)
Clause1 clause2 ...)
(If test
(Begin result1 result2 ...)
(Cond clause1 clause2 ...)))))

Based on the cond definition above, the following code

(Let (=> # F ))
(Cond (# T => 'OK); =) OK

It will be interpreted as (here => is regarded as a variable rather than a syntax element)

(Let (=> # F ))
(If # T (begin => 'OK )))

Instead

(Let (=> # F ))
(Let (temp # t ))
(If temp ('OK temp ))))

Because the local => has changed its meaning, it cannot match the syntax rules that contain =>.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.