scheme语言里cons?scheme是什么意思思

第三方登录:Scheme - Expressions
Go to the , , ,
section, .
A Scheme expression is a construct that returns a value, such as a
variable reference, literal, procedure call, or conditional.
Expression types are categorized as primitive or derived.
Primitive expression types include variables and procedure calls.
Derived expression types are not semantically primitive, but can instead
be explained in terms of the primitive constructs as in
They are redundant in the strict sense of
the word, but they capture common patterns of usage, and are therefore
provided as convenient abbreviations.
essential syntax: <variable>
An expression consisting of a variable
(section ) is a variable reference.
The value of
the variable reference is the value stored in the location to which the
variable is bound.
It is an error to reference an
(define x 28)
essential syntax: quote <datum>
essential syntax: '<datum>
essential syntax: <constant>
(quote <datum>) evaluates to <datum>.
<Datum> may be any external representation of a Scheme object (see
section ).
This notation is used to
include literal constants in Scheme code.
(quote #(a b c))
(quote (+ 1 2))
(quote <datum>) may be abbreviated as
'<datum>.
The two notations are equivalent in all
'(quote a)
Numerical constants, string constants, character constants, and boolean
constants evaluate "to themselves"; they need not be quoted.
As noted in section , it is an error to alter a constant
(i.e. the value of a literal expression) using a mutation procedure like
set-car! or string-set!.
essential syntax: <operator> <operand 1> ...
A procedure call is written by simply enclosing in parentheses
expressions for the procedure to be called and the arguments to be
passed to it.
The operator and operand expressions are evaluated (in an
unspecified order) and the resulting procedure is passed the resulting
arguments.
((if #f + *) 3 4)
A number of procedures are available as the values of variables in the
for example, the addition and multiplication
procedures in the above examples are the values of the variables
New procedures are created by evaluating lambda expressions (see section
section ).
Procedure calls are also called combinations.
In contrast to other dialects of Lisp, the order of
evaluation is unspecified, and the operator expression and the operand
expressions are always evaluated with the same evaluation rules.
Although the order of evaluation is otherwise unspecified, the effect of
any concurrent evaluation of the operator and operand expressions is
constrained to be consistent with some sequential order of evaluation.
The order of evaluation may be chosen differently for each procedure call.
In many dialects of Lisp, the empty combination,
(), is a legitimate expression.
In Scheme, combinations must
least one subexpression, so () is not a syntactically valid
expression.
essential syntax: lambda <formals> <body>
<Formals> should be a formal arguments list as described below,
and <body> should be a sequence of one or more expressions.
Semantics:
A lambda expression evaluates to a procedure.
The environment in
effect when the lambda expression was evaluated is remembered as part of the
procedure.
When the procedure is later called with some actual
arguments, the environment in which the lambda expression was evaluated will
be extended by binding the variables in the formal argument list to
fresh locations, the corresponding actual argument values will be stored
in those locations, and the expressions in the body of the lambda expression
will be evaluated sequentially in the extended environment.
The result
of the last expression in the body will be returned as the result of
the procedure call.
(lambda (x) (+ x x))
a procedure
((lambda (x) (+ x x)) 4)
(define reverse-subtract
(lambda (x y) (- y x)))
(reverse-subtract 7 10)
(define add4
(let ((x 4))
(lambda (y) (+ x y))))
<Formals> should have one of the following forms:
(<variable 1> ...):
The procedure takes a fixed when the procedure is
called, the arguments will be stored in the bindings of the
corresponding variables.
<variable>:
The procedure takes any when the procedure is
called, the sequence of actual arguments is converted into a newly
allocated list, and the list is stored in the binding of the
<variable>.
(<variable 1> ... <variable n-1> . <variable n>):
If a space-delimited period precedes the last variable, then
the value stored in the binding of the last variable will be a
newly allocated
list of the actual arguments left over after all the other actual
arguments have been matched up against the other formal arguments.
It is an error for a <variable> to appear more than once in
<formals>.
((lambda x x) 3 4 5 6)
((lambda (x y . z) z)
Each procedure created as the result of evaluating a lambda expression
is tagged with a storage location, in order to make eqv? and
eq? work on procedures (see section ).
essential syntax: if <test> <consequent> <alternate>
syntax: if <test> <consequent>
<Test>, <consequent>, and <alternate> may be arbitrary
expressions.
Semantics:
An if expression is evaluated as follows: first,
<test> is evaluated.
If it yields a true value
(see section ), then <consequent> is evaluated
and its value is returned.
Otherwise <alternate> is evaluated and
its value is returned.
If <test> yields a false value and no
<alternate> is specified, then the result of the expression is
unspecified.
(if (> 3 2) 'yes 'no)
(if (> 2 3) 'yes 'no)
(if (> 3 2)
essential syntax: set! <variable> <expression>
<Expression> is evaluated, and the resulting value is stored in
the location to which <variable> is bound.
<Variable> must
be bound either in some region
enclosing the set! expression
or at top level.
The result of the set! expression is
unspecified.
(define x 2)
(set! x 4)
unspecified
For reference purposes, section
gives rewrite rules
that will convert constructs described in this section into the
primitive constructs described in the previous section.
essential syntax: cond <clause 1> <clause 2> ...
Each <clause> should be of the form
(<test> <expression> ...)
where <test> is any expression.
The last <clause> may be
an "else clause," which has the form
(else <expression 1> <expression 2> ...).
Semantics:
A cond expression is evaluated by evaluating the <test>
expressions of successive <clause>s in order until one of them
evaluates to a true value
(see section ).
When a <test> evaluates to a
true value, then the remaining <expression>s in its <clause> are
evaluated in order, and the result of the last <expression> in the
<clause> is returned as the result of the entire cond
expression.
If the selected <clause> contains only the <test>
and no <expression>s, then the value of the <test> is returned
as the result.
If all <test>s evaluate to false values, and there
is no else clause, then the result of the conditional expression is
if there is an else clause, then its <expression>s are
evaluated, and the value of the last one is returned.
(cond ((> 3 2) 'greater)
((< 3 2) 'less))
(cond ((> 3 3) 'greater)
((< 3 3) 'less)
(else 'equal))
Some implementations support an alternative <clause> syntax,
(<test> => <recipient>), where <recipient> is an
expression.
If <test> evaluates to a true value, then
<recipient> is evaluated.
Its value must be a procedure of one
this procedure is then invoked on the value of the
<test>.
(cond ((assv 'b '((a 1) (b 2))) => cadr)
(else #f))
essential syntax: case <key> <clause 1> <clause 2> ...
<Key> may be any expression.
Each <clause> should have
((<datum 1> ...) <expression 1> <expression 2> ...),
where each <datum> is an external representation of some object.
All the <datum>s must be distinct.
The last <clause> may be an "else clause," which has the form
(else <expression 1> <expression 2> ...).
Semantics:
A case expression is evaluated as follows.
<Key> is
evaluated and its result is compared against each <datum>.
result of evaluating <key> is equivalent (in the sense of
eqv?; see section ) to a <datum>, then the
expressions in the corresponding <clause> are evaluated from left
to right and the result of the last expression in the <clause> is
returned as the result of the case expression.
If the result of
evaluating <key> is different from every <datum>, then if
there is an else clause its expressions are evaluated and the
result of the last is the result of the case
the result of the case expression is unspecified.
(case (* 2 3)
((2 3 5 7) 'prime)
((1 4 6 8 9) 'composite)) =>
(case (car '(c d))
unspecified
(case (car '(c d))
((a e i o u) 'vowel)
((w y) 'semivowel)
(else 'consonant))
essential syntax: and <test 1> ...
The <test> expressions are evaluated from left to right, and the
value of the first expression that evaluates to a false value (see
section ) is returned.
Any remaining expressions
are not evaluated.
If all the expressions evaluate to true values, the
value of the last expression is returned.
If there are no expressions
then #t is returned.
(and (= 2 2) (> 2 1))
(and (= 2 2) (< 2 1))
(and 1 2 'c '(f g))
essential syntax: or <test 1> ...
The <test> expressions are evaluated from left to right, and the value of the
first expression that evaluates to a true value (see
section ) is returned.
Any remaining expressions
are not evaluated.
If all expressions evaluate to false values, the
value of the last expression is returned.
If there are no
expressions then #f is returned.
(or (= 2 2) (> 2 1))
(or (= 2 2) (< 2 1))
(or #f #f #f)
(or (memq 'b '(a b c))
The three binding constructs let, let*, and letrec
give Scheme a block structure, like Algol 60.
The syntax of the three
constructs is identical, but they differ in the regions
they establish
for their variable bindings.
In a let expression, the initial
values are computed before any of the va in a
let* expression, the bindings and evaluations are performed
while in a letrec expression, all the bindings are
effect while their initial values are being computed, thus allowing
mutually recursive definitions.
essential syntax: let <bindings> <body>
<Bindings> should have the form
((<variable 1> <init 1>) ...),
where each <init> is an expression, and <body> should be a
sequence of one or more expressions.
an error for a <variable> to appear more than once in the list of variables
being bound.
Semantics:
The <init>s are evaluated in the current environment (in some
unspecified order), the <variable>s are bound to fresh locations
holding the results, the <body> is evaluated in the extended
environment, and the value of the last expression of <body> is
Each binding of a <variable> has <body> as its
(let ((x 2) (y 3))
(let ((x 2) (y 3))
(let ((x 7)
(z (+ x y)))
See also named let, section .
syntax: let* <bindings> <body>
<Bindings> should have the form
((<variable 1> <init 1>) ...),
and <body> should be a sequence of
one or more expressions.
Semantics:
Let* is similar to let, but the bindings are performed
sequentially from left to right, and the region
of a binding indicated
by (<variable> <init>) is that part of the let*
expression to the right of the binding.
Thus the second binding is done
in an environment in which the first binding is visible, and so on.
(let ((x 2) (y 3))
(let* ((x 7)
(z (+ x y)))
essential syntax: letrec <bindings> <body>
<Bindings> should have the form
((<variable 1> <init 1>) ...),
and <body> should be a sequence of
one or more expressions. It is an error for a <variable> to appear more
than once in the list of variables being bound.
Semantics:
The <variable>s are bound to fresh locations
holding undefined values, the <init>s are evaluated in the resulting
environment (in some unspecified order), each <variable> is assigned
to the result of the corresponding <init>, the <body> is
evaluated in the resulting environment, and the value of the last
expression in <body> is returned.
Each binding of a <variable>
has the entire letrec expression as its region , making it
possible to define mutually recursive procedures.
(letrec ((even?
(lambda (n)
(if (zero? n)
(odd? (- n 1)))))
(lambda (n)
(if (zero? n)
(even? (- n 1))))))
(even? 88))
One restriction on letrec is very important: it must be possible
to evaluate each <init> without assigning or referring to the value of any
<variable>.
If this restriction is violated, then it is an error.
restriction is necessary because Scheme passes arguments by value rather than by
In the most common uses of letrec, all the <init>s are
lambda expressions and the restriction is satisfied automatically.
essential syntax: begin <expression 1> <expression 2> ...
The <expression>s are evaluated sequentially from left to right,
and the value of the last <expression> is returned.
expression type is used to sequence side effects such as input and
(define x 0)
(begin (set! x 5)
(begin (display "4 plus 1 equals ")
(display (+ 4 1)))
unspecified
and prints
4 plus 1 equals 5
uses the keyword sequence instead of begin.
syntax: do <bindings> <clause> <body>
<Bindings> should have the form
((<variable 1> <init 1> <step 1>) ...),
<clause> should be of the form
(<test> <expression> ...),
and <body> should be a sequence of one or more expressions.
Do is an iteration construct.
It specifies a set of variables to
be bound, how they are to be initialized at the start, and how they are
to be updated on each iteration.
When a termination condition is met,
the loop exits with a specified result value.
Do expressions are evaluated as follows:
The <init> expressions are evaluated (in some unspecified order),
the <variable>s are bound to fresh locations, the results of the
<init> expressions are stored in the bindings of the
<variable>s, and then the iteration phase begins.
Each iteration begins by evaluating <test>; if the result is
false (see section ), then the <command>
expressions are evaluated in order for effect, the <step>
expressions are evaluated in some unspecified order, the
<variable>s are bound to fresh locations, the results of the
<step>s are stored in the bindings of the
<variable>s, and the next iteration begins.
If <test> evaluates to a true value, then the
<expression>s are evaluated from left to right and the value of
the last <expression> is returned as the value of the do
expression.
If no <expression>s are present, then the value of
the do expression is unspecified.
The region
of the binding of a <variable> consists of the entire do
expression except for the <init>s.
It is an error for a
<variable> to appear more than once in the list of do
variables.
A <step> may be omitted, in which case the effect is the
same as if (<variable> <init> <variable>) had
been written instead of (<variable> <init>).
(do ((vec (make-vector 5))
(i 0 (+ i 1)))
((= i 5) vec)
(vector-set! vec i i))
#(0 1 2 3 4)
(let ((x '(1 3 5 7 9)))
(do ((x x (cdr x))
(sum 0 (+ sum (car x))))
((null? x) sum)))
syntax: let <variable> <bindings> <body>
Some implementations of Scheme permit a variant on the syntax of
let called "named let" which provides a more general
looping construct than do, and may also be used to express
recursions.
Named let has the same syntax and semantics as ordinary
let except that <variable> is bound within <body> to a
procedure whose formal arguments are the bound variables and whose body
is <body>.
Thus the execution of <body> may be repeated by
invoking the procedure named by <variable>.
(let loop ((numbers '(3 -2 1 6 -5))
(nonneg '())
(neg '()))
(cond ((null? numbers) (list nonneg neg))
((>= (car numbers) 0)
(loop (cdr numbers)
(cons (car numbers) nonneg)
((< (car numbers) 0)
(loop (cdr numbers)
(cons (car numbers) neg)))))
((6 1 3) (-5 -2))
syntax: delay <expression>
The delay construct is used together with the
procedure force to
implement lazy evaluation or call by need.
(delay <expression>) returns an object called a
promise which at some point in the future may be asked (by
the force procedure)
to evaluate <expression> and deliver the resulting value.
See the description of force (section ) for a
more complete description of delay.
essential syntax: quasiquote <template>
essential syntax: ` <template>
"Backquote" or "quasiquote"
expressions are useful
for constructing a list or vector structure when most but not all of the
desired structure is known in advance.
appear within the <template>, the result of evaluating
`<template> is equivalent to the result of evaluating
'<template>.
If a comma
appears within the
<template>, however, the expression following the comma is
evaluated ("unquoted") and its result is inserted into the structure
instead of the comma and the expression.
If a comma appears followed
immediately by an at-sign (@),
then the following
expression mus the opening and closing parentheses
of the list are then "stripped away" and the elements of the list are
inserted in place of the comma at-sign expression sequence.
`(list ,(+ 1 2) 4)
(list 3 4)
(let ((name 'a)) `(list ,name ',name))
(list a (quote a))
`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)
(a 3 4 5 6 b)
`((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))
((foo 7) . cons)
`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8)
#(10 5 2 4 3 8)
Quasiquote forms may be nested.
Substitutions are made only for
unquoted components appearing at the same nesting level
as the outermost backquote.
The nesting level increases by one inside
each successive quasiquotation, and decreases by one inside each
unquotation.
`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
(a `(b ,(+ 1 2) ,(foo 4 d) e) f)
(let ((name1 'x)
(name2 'y))
`(a `(b ,,name1 ,',name2 d) e))
(a `(b ,x ,'y d) e)
The notations `<template> and
(quasiquote <template>) are identical in all respects.
,<expression> is identical to (unquote
<expression>), and ,<expression> is identical to
(unquote-splicing <expression>).
The external syntax
generated by write for two-element lists whose
car is one of these symbols may vary between implementations.
(quasiquote (list (unquote (+ 1 2)) 4))
(list 3 4)
'(quasiquote (list (unquote (+ 1 2)) 4))
`(list ,(+ 1 2) 4)
i.e., (quasiquote (list (unquote (+ 1 2)) 4))
Unpredictable behavior can result if any of the symbols
quasiquote, unquote, or unquote-splicing
positions within a <template> otherwise than as described above.
Go to the , , ,
section, .为什么Scheme语言中and和or不是函数,而not是函数?_百度知道
为什么Scheme语言中and和or不是函数,而not是函数?
……看到你是在国外学习! 好好听你们老师的吧!不同的语言 都有自己的 规则……但是 就你所说的这几个 单从 词语的角度来看 在一个语言中 它们应该被划为同一类……所以你需要进一步与你的老师 确认那是真的这个 没什么 好纠结的,你要及时提出来哦,这一方面的疑问
其他类似问题
为您推荐:
其他3条回答
IF(AND(没那么复杂: =if(j6=0,25,40,IF(OR(J6=0%);40&&quot,if(j6&=10%我写的公式是,if(j6&lt:IF(ISBLANK(E6);,35;=2%,&,&quot
我在国外学cs,用的scheme语言,老师上课说and和or不是function只是operator,而not是function,我就不明白了,求解释
operator的本质还是函数,你这样的情况,是这个语言的解释器硬性规定的,就像语言大多以Main做为入口函数一样..
and 和 or 不是函数,就可以短路求值,而 not 只有一个参数,总是要求值的.例如 (or (string? &aaa&) (/ 1 0)), 如果 or 是个函数,(/ 1 0) 总是要求值的。
scheme的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 url scheme是什么意思 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信