Branch
if expression
if
The expression process is divided into two parts. if
The format is as follows:
(if predicate then_value else_value)
If the predicate
part is true, then then_value
part is evaluated , or else_value
partially evaluated, and the value found is returned to if
the outer brackets statement.
true
It is in addition to false
any value other than true
the use #t
indicated false
by #f
expressed .
In R5RS in false
( ) #f
and empty tables (’())
are two different objects. However, in the MIT-Scheme, both for the same object.
This difference may be a problem left over from history, the previous standard --R4RS in, #f
and ’()
is defined as the same object.
Therefore, from a compatibility point of view, you should not use the directory table as predicates. Using the function null?
to determine whether the table is empty .
1 ]=> (null? `()) ;Value: #t 1 ]=> (null? `(1 2 3)) ;Value: #f 1 ]=> (null? `(a b c)) ;Value: #f
Functionnot
can be used to invert the predicate. This takes one argument, and if the parameter value#f
is returned#t
, on the contrary, the parameter value#t
is returned#f
.if
Expression is a special form, because it does not all parameters evaluated.
Because ifpredicate
true, onlythen_value
partially evaluated.
On the other hand, ifpredicate
false, onlyelse_value
partially evaluated.
Example: The first item is a0
, the growth rate of r
the number of entries for the n
geometric growth (geometric progression) of the sum of the number of columns
(define (sum-gp a0 r n) (* a0 (if (= r 1) n (/ (- 1 (expt r n)) (- 1 r))))) ; !!
1 ]=> (define sum-gp (lambda (a0 r n) (* a0 (if (= r 1) n (/ (- 1 (expt r n) (- 1 r))))))) ;Value: sum-gp 1 ]=> (sum-gp 2 1 3) ;Value: 6
In general, the geometric growth in the number of columns summation formula is as follows:
a0 * ( 1 - r ^ n) / ( 1 - r) (r ≠ 1 ) a0 * n (r = 1 )
If the if
expression is evaluated for all parameters, then there is ;!!
a comment that even in line r=1
will be seeking value,
which will result in a "divide by zero" error.
You can save else_value
items. In this case, when predicate
the time is false, the return value is not specified.
If you want to when predicate
the return is false #f
, then it would have to explicitly write it.
then_value
And else_value
it should be S- expressions .
If you need side effect, then you should use begin
the expression . We will discuss in the next chapter begin
expression.
and and or (heavy and difficult)
and
And or
for the combination condition two special form.
Scheme of and
and or
different from the C language conventions. They do not return a boolean value ( #t
or a #f
), but returns one of the given parameters.
and
Having any number of arguments, and from left to right to find the value thereof .
If a parameter is #f
, then it returns #f
, without evaluating the remaining parameters. ? ? Traversal
Conversely, if all parameters are not#f
, then the return value of the last parameter .
1 ]=> (and #f 0) ;Value: #f 1 ]=> (or #f 0) ;Value: 0
1 ]=> (and 1 2 3) ;Value: 3 1 ]=> (or 1 2 3) ;Value: 1
1 ]=> (and 1 2 3 #f) ;Value: #f 1 ]=> (or 1 2 3 #f) ;Value: 1
1 ]=> (and #f #f) ;Value: #f 1 ]=> (or #f #f) ;Value: #f 1 ]=> (or #f #f #f) ;Value: #f
or
Having a variable number of arguments, and they evaluated from left to right.
It returns a value that is not the first #f
parameter, while the remaining parameters are not evaluated. The first value after the return #f. ?? Traversal
If all the values of the parameters are #f
, then the return value of the last parameter . ? ? Naturally is # F
note! !
1 ]=> (or #f 34 #f) ;Value: 34 1 ]=> (or #f 23 45 #f) ;Value: 23
In contrast, summed up the law
1 ]=> (or #f 12 23 45) ;Value: 12 1 ]=> (or #f 12 34 45 #f) ;Value: 12
Exercise 2
Write the following function.
- Receiving a real number as a function of three parameters, if the parameters are all positive their product is returned .
- Receiving a real number as a function of three parameters, they are negative if the product is returned to at least one parameter.
------ first time to write, failure -----
1 ]=> (define p2 (lambda (a b c) (if (and (> a 0) (> b 0) (> c 0) #f) (a * b *c)) #f)) ;Value: p2 1 ]=> (p2 2 3 4) ;Value: #f
----- preparation of the second, failed ---
1 ]=> (define p3 (lambda (a b c) (if ((> a 0) (> b 0) (> c 0)) (a * b * c) (a + b + c)) )) ;Value: p3 1 ]=> (p3 2 3 4) ;The object #t is not applicable. ;To continue, call RESTART with an option number: ; (RESTART 2) => Specify a procedure to use in its place. ; (RESTART 1) => Return to read-eval-print level 1.
------- ---- certainly not thinking there is something missing
1 ]=> (define p4 (lambda (a b c) (if (and (> a 0) (> b 0) (> c 0)) (a * b * c) (a + b +c)) )) ;Value: p4 1 ]=> (p4 2 3 4) ;The object 2 is not applicable. ;To continue, call RESTART with an option number: ; (RESTART 2) => Specify a procedure to use in its place. ; (RESTART 1) => Return to read-eval-print level 1.
cond expression
Although all branches can use if
the expression expression, but when conditions are more possibilities, you need to use nested if
expressions , and this will complicate the code. Deal with this situation you can use cond
expressions. cond
Expression format is as follows:
(cond (predicate_1 clauses_1) (predicate_2 clauses_2) ...... (predicate_n clauses_n) (else clauses_else))
In cond
the expression, predicates_i
it is evaluated from the top down, and when predicates_i
the time is true, clause_i
is evaluated and returned.
i
After predicates
and clauses
will not be evaluated . ? ? ?
If all predicates_i
are false, then return cluase_else
.
In a clause, you can write the number of S- expressions, and clause
the value is the last S- expressions.
Function to make judgments
The basic functions eq?
, eqv?
, equal?
with two parameters, the two parameters used to check whether the "match." Some slight differences between these three functions.
eq?
This function compares two objects address , if the same, then returns#t
.For example,
(eq? str str)
to return#t
, becausestr
the address itself is consistent.On the other hand, because the strings
”hello”
and”hello”
different addresses are stored in a, the function will return#f
.Do not use
eq?
to compare numbers , because only in R5RS in, even in the MIT-Scheme implementation, it did not specify a return value. Useeqv?
or=
replacement.
eqv?
This function compares two stored in the object memory type and value .If the values are the same type and then returns
#t
.Process for the (
lambda
expression) is more dependent on the specific implementation.This function can not be used to sequence and a type similar to Table string comparison , because, although these sequences appear to be identical, but their values are stored in different addresses.
equal?
This function is used to compare a string or a sequence similar to Table class .
1 ]=> (define str "hello Schemer") ;Value: str 1 ]=> str ;Value 18: "hello Schemer" 1 ]=> (eq? str str) ;Value: #t 1 ]=> (str) ;The object "hello Schemer" is not applicable. ;To continue, call RESTART with an option number: ; (RESTART 2) => Specify a procedure to use in its place. ; (RESTART 1) => Return to read-eval-print level 1. 2 error> ^G ;Quit! 1 ]=> str ;Value 18: "hello Schemer" 1 ]=> (eq? "hello Schemer" "hello Schemer") ;Value: #f
1 ]=> (eqv? 1 1) ;Value: #t 1 ]=> (eqv? 1 1.0) ;Value: #f 1 ]=> (eqv? (1 2 3) (1 2 3)) ;The object 1 is not applicable. ;To continue, call RESTART with an option number: ; (RESTART 2) => Specify a procedure to use in its place. ; (RESTART 1) => Return to read-eval-print level 1. 2 error> (eqv? (list 1 2 3) (list 1 2 3)) ;Value: #f
1 ]=> (equal? (1 2 3) (1 2 3)) ;The object 1 is not applicable. ;To continue, call RESTART with an option number: ; (RESTART 2) => Specify a procedure to use in its place. ; (RESTART 1) => Return to read-eval-print level 1. 2 error> ^G ;Quit! 1 ]=> (equal? (list 1 2 3) (list 1 2 3)) ;Value: #t 1 ]=> (equal? "hello" "hello") ;Value: #t
A function for checking data type
Here are several functions for checking types. These functions are only one parameter.
pair?
If the object is to return sequence#t
;list?
If the object is a table are returned#t
. Be careful that the empty table’()
is a table but not a sequence right.null?
If the object list is empty '(), then returns #t.symbol?
If the object is a symbol #t is returned.char?
If the object is a character #t is returned.string?
If the object is a string #t is returned.number?
If the object is a digital #t is returned.complex?
If the object is a complex #t is returned.real?
If the object is a real number #t is returned.rational?
If the object is a rational #t is returned.integer?
If the object is an integer #t is returned.exact?
If the object is not a floating-point number, then #t is returned.inexact?
If the object is a floating-point number, then #t is returned.
Comparison of a function of the number of
=
, >
, <
, <=
, >=
These functions have arbitrary number of arguments. If the argument is sorted according to the name of these functions, then the function returns#t
.
odd?
, even?
, positive?
, negative?
, zero?
These functions are only one argument , if these conditions are satisfied, then the function name parameters indicated on the return #t
.
Function used to compare symbols
It can be used when comparing the characters char=?
, char<?
, char>?
, char<=?
and char>=?
functions. Specific details, see R6RS.
算法语言Scheme修订6报告 R6RS简体中文翻译,https://r6rs.mrliu.org/
用于比较字符串的函数
比较字符串时,可以使用string=?
和string-ci=?
等函数。具体细节请参见R6RS。