📜  LISP 中的 Cond 构造

📅  最后修改于: 2022-05-13 01:54:23.669000             🧑  作者: Mango

LISP 中的 Cond 构造

在本文中,我们将讨论 LISP 中的 cond 构造。 cond 是用于制定n个测试条件的决策语句。它将检查所有条件。

语法

(cond   (condition1 statements)
(condition2 statements)
(condition3 statements)
...
(conditionn statements)
)

这里,

  1. 条件指定不同的条件——如果不满足条件 1,则继续下一个条件 IE 条件,直到最后一个条件。
  2. 语句指定基于条件完成的工作。

注意:它将只执行一条语句。

示例 1 :检查数字是否大于 200 的 LISP 程序

Lisp
;set value1 to 500
(setq val1 500)
  
;check whether the val1 is greater than 200
(cond ((> val1 200)
   (format t "Greater than 200"))
   (t (format t "Less than 200")))


Lisp
;set value1 to 500
(setq val1 500)
  
;check whether the val1 is greater  than 200
(cond ((> val1 200)
   (format t "Greater than 200"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is equal to 500
(cond ((= val1 500)
   (format t "equal to 500"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is equal to 600
(cond ((= val1 600)
   (format t "equal to 500"))
   (t (format t "Not")))
 (terpri)
   
 ;check whether the val1 is greater than or equal to 400
(cond ((>= val1 400)
   (format t "greater than or equal to 400"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is less than or equal to 600
(cond ((<= val1 600)
   (format t "less than or equal to 600"))
   (t (format t "Not")))


输出:

Greater than 200

示例 2:带有运算符的演示

语言

;set value1 to 500
(setq val1 500)
  
;check whether the val1 is greater  than 200
(cond ((> val1 200)
   (format t "Greater than 200"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is equal to 500
(cond ((= val1 500)
   (format t "equal to 500"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is equal to 600
(cond ((= val1 600)
   (format t "equal to 500"))
   (t (format t "Not")))
 (terpri)
   
 ;check whether the val1 is greater than or equal to 400
(cond ((>= val1 400)
   (format t "greater than or equal to 400"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is less than or equal to 600
(cond ((<= val1 600)
   (format t "less than or equal to 600"))
   (t (format t "Not")))

输出:

Greater than 200
equal to 500
Not
greater than or equal to 400
less than or equal to 600