简体   繁体   English

如果在球拍中

[英]Cond If And Or in Racket

Can someone please explain these functions for me in racket. 有人可以在球拍中为我解释这些功能。 I`m totally lost. 我完全迷路了。 Please help me with some examples. 请帮我举一些例子。 Thanks! 谢谢! I just cannot figure these functions out for the life of me. 我只是一辈子都无法弄清楚这些功能。

First, the If: 首先,如果:

(if (positive? 1) 1 -1)

Racket first evaluates if 1 is positive (which is the first expresion (positive? 1)). 球拍首先评估1是否为正(这是第一个表达式(正?1))。 If it is, returns 1, else, return -1. 如果是,则返回1,否则返回-1。 This is equivalent to c-like languages doing: 这等效于类似c的语言:

if ( positive?(1))
  return 1
else
  return -1

The Cond is basically a if that has multiple options. 如果有多个选项,Cond基本上是一个条件。 The equivalent in C-like languages would be else-if 如果类似的话,类似C的语言将是

(cond [(first-condition) (what-to-do)]
      [(second-condition) (what-to-do)]
      [(third-condition) (you-get-the-idea)])

And and Or are just the logical operators, equivalent to && and || And和Or只是逻辑运算符,等效于&&和|| in C-like languages 类似C的语言

(and true true) => true
(and true false) => false

(or true true) => true
(or true false) => true
(or false false) => false

If is the turnary operator. If是临时运算符。 It has three arguments, unless the first argument has a value of #f it will return the value of the second argument, otherwise the value of the third argument. 它具有三个参数,除非第一个参数的值是#f否则它将返回第二个参数的值,否则将返回第三个参数的值。

OR accepts one or more arguments, evaluates them one at a time left to right, and returns the first value if finds that is not #f , and returns #f if no argument satisfies that condition. OR接受一个或多个参数,一次从左到右对其求值,如果发现不是#f ,则返回第一个值,如果没有参数满足该条件,则返回#f

AND accepts one or more arguments evaluate them one at a time left to right, if it finds one that is #f it returns #f , otherwise it returns the value of the last argument that it evaluates. AND接受一个或多个参数从左到右一次对它们求值,如果找到#f则返回#f ,否则返回它求值的最后一个参数的值。

COND accepts one or more arguments, each with one or more sub arguments (2 is the usual number though). COND接受一个或多个参数,每个参数都带有一个或多个子参数(不过2是通常的数字)。 It evaluates each argument left to right one at a time by evaluating the first subargument. 它通过评估第一个子参数来一次评估从左到右的每个参数。 If it is not #f then it evaluates each of the rest of the sub-arguments (if any) in order and returns the value of the last argument. 如果不是#f则它将按顺序评估其余所有子参数(如果有),并返回最后一个参数的值。 Otherwise it moves onto the next argument and evaluates it the same way. 否则,它将移至下一个参数,并以相同的方式对其求值。 else is a special sytax within these sub-arguments that is always treated as if it is not #f . else是这些子参数中的特殊语法,始终被视为不是#f

And here argument is understood to mean any valid s-expression. 这里的论点被理解为表示任何有效的s表达式。

NB: If you are familiar with non-lisp languages this is the answer for you. 注意:如果您熟悉非lisp语言,这就是您的答案。 I'm not trying to explain them in other way than other code. 我不是在尝试用其他代码来解释它们。 Other answers do however so this is just a supplement. 但是,其他答案也可以,所以这只是一个补充。

None of those are functions, but special forms. 这些都不是功能,而是特殊形式。

(if predicate 
    consequent 
    alternative)

is pretty much like Algol-dialects if 是很像大陵方言if

if( predicate )
{
   consequent
}
else
{
   alternative
}

Note that predicate, consequent and alternative can be anything from a complex expression to a simple value. 请注意,谓词,结果和替代可以是任何值,从复杂的表达式到简单的值。

cond works more like an if, elseif..., else: cond工作方式更类似于if,elseif ...,else:

(cond (predicate1 consequent1)
      (predicaten consequentn)
      (else alternative))

and works like && in algol dialects. and在阿尔方言中像&&一样工作。 Thus false && print("Do it!") doesn't print anything since it short circuits. 因此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 works like || or || in algol dialects. 在方言中。 thus true || print("Do it!") true || print("Do it!") true || print("Do it!") doesn't print since first term was true. 因为第一项为真, true || print("Do it!")不打印。

(or 'mama (display "Do it")) ; ==> mama (first true value, does not print "do it")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM