誰かがラケットで私のためにこれらの機能を説明してもらえますか. 私は完全に迷っています。いくつかの例を教えてください。ありがとう!私は一生、これらの機能を理解することはできません。
3 に答える
まず、If:
(if (positive? 1) 1 -1)
Racket は最初に 1 が正かどうかを評価します (最初の式 (正? 1) です)。そうである場合は 1 を返し、そうでない場合は -1 を返します。これは、次のことを行う C ライクな言語と同等です。
if ( positive?(1))
return 1
else
return -1
Cond は基本的に、複数のオプションを持つ if です。C ライクな言語で同等のものは、else-if です。
(cond [(first-condition) (what-to-do)]
[(second-condition) (what-to-do)]
[(third-condition) (you-get-the-idea)])
And と Or は単なる論理演算子で、&& と || に相当します。Cライクな言語で
(and true true) => true
(and true false) => false
(or true true) => true
(or true false) => true
(or false false) => false
注意: 非 Lisp 言語に精通している場合は、これが答えです。他のコード以外の方法でそれらを説明しようとしているわけではありません。ただし、他の回答もあるため、これは単なる補足です。
それらはどれも関数ではなく、特別な形式です。
(if predicate
consequent
alternative)
アルゴール方言によく似ているif
if( predicate )
{
consequent
}
else
{
alternative
}
predicate、consequent、alternative は、複雑な式から単純な値まで何でもかまいません。
cond
if、elseif...、else のように機能します。
(cond (predicate1 consequent1)
(predicaten consequentn)
(else alternative))
and
&&
algol 方言のように機能します。したがってfalse && print("Do it!")
、短絡しているため、何も出力されません。
(and #f (display "Do it!")) ; ==> #f (and does not display anything since first term was false)
(and 1 2 3) ; ==> 3 (3 is the last true value. In Scheme everything except #f is true.)
or
||
algol 方言のように機能します。したがってtrue || print("Do it!")
、最初の用語が真だったので印刷されません。
(or 'mama (display "Do it")) ; ==> mama (first true value, does not print "do it")