Delimiter and Keyword in srfi-187
Joo ChurlSoo 17 Mar 2020 06:31 UTC
1. Delimiter
I fixed a few fundamental flaws, and revised the specification and sample
implementation to clarify how to use the variable delimiter.
This is also for people who would like to use the delimiter as a required
variable or an optional variable.
(alambda <rnrs formals> <body>) ;The alambda is the same as the lambda.
(alambda <extended formals> <body>)
<extended formals> --> <variable>
| (<required spec>*)
| (<required spec>+ <rest spec>)
| (<required spec>* & <optional spec>* <rest spec>)
& --> <delimiter> | <variable>
The ampersand character is not only used as a variable delimiter but also as
an ordinary variable. In other words, it becomes a variable delimiter if syntax
requires distinction between required and optional variables, and also becomes
an ordinary variable if not.
Examples to clarify how to use the variable delimiter:
((alambda (a b &) ;The ampersand is a required variable.
(list a b &))
1 2 3)
=> (1 2 3)
((alambda ((& #t) ,& &) ;(& #t) required sequent variable
&) ;,& required unnamed variable
1 2 3) ;& required sequent variable
=> 3
((alambda ((& #t) ,& &) ;Both (& #t) and ,& are <extended formals>.
&)
1 2)
=> error: no argument for required sequent variable &
((alambda (& & & . e) ;1st &: delimiter (This is not <rnrs formals>)
(list & e)) ;2nd &: optional sequent variable
1 2 3) ;3rd &: optional sequent variable
=> (2 (3))
((alambda (& . &) ;This is <rnrs formals>.
(list &))
1 2 3)
=> error: Duplicate parameter in parameter list
((alambda (& . e) ;The ampersand is a required variable.
(list & e))
1 2 3)
=> (1 (2 3))
((alambda (a b c a b c)
(list a b c))
1 2 3 4 5 6)
=> error: Duplicate parameter in parameter list
((alambda (a b c `a b c) ;Duplication of variables is permitted
(list a b c)) ;only after an extended variable of
1 2 3 4 5 'a 6) ;<extended formals> appears.
=> (6 4 5)
However, the implementation is written only with define-macro, because it
cannot be implemented with define-syntax(syntax-rules).
2. keyword
In SRFI-187, Keywords are real beings that deliver values to variables in a
function. But the keyword itself knows that it cannot serve as a keyword by
itself and must be accompanied by a value. In other words, It knows that there
is no meaning of existence without the accompanying value.
For example:
(adefine (str-ref-key 'str 'num)
(string-ref str num))
(str-ref-key 'num 1 'str "str")
=> #\t
(adefine (str-ref-key (('str #:str)) (('num #:num)))
(string-ref str num))
(str-ref-key #:num 1 #:str "str")
=> #\t
(adefine (str-ref-key (('str #:str)) (('num #:num)) . rem)
(string-ref str num))
(str-ref-key #:num 1 #:str "str" #:rem)
=> #\t ;The #:rem is in rem,
;because keywords are real beings.
(adefine (str-ref-key
(('str "The value of the argument next to me is the value of str" equal?))
(('num "The value of the argument next to me is the value of num" equal?)))
(string-ref str num)) ;Stictly speaking,
;it is not a keyword but a keystring.
(str-ref-key "The value of the argument next to me is the value of num" 1
"The value of the argument next to me is the value of str" "str")
=> #\t
The str-ref-key doesn't requires two ;The str-ref-key acknowledges the
but four arguments, ;existence of keywords.
and uses only two of them. ;This is not ignoring the keyword,
;but respecting its role.
Keywords in SRFI-187 are just like this.
--
Joo ChurlSoo