📜  LISP 中的决策

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

LISP 中的决策

决策用于指定在 LISP 中评估表达式的条件。

LISP 中有 4 种类型的决策语句。他们是

  • 如果
  • 条件
  • 什么时候
  • 案子

if 语句

if 是一个决策语句,用于检查条件是对还是错。如果条件正确,则进入 if 块内部,执行 if 块下的语句。否则,不执行语句。

句法:

(if (condition) then (statement 1).....(statement  n))

在这里,then 是 if 语句中使用的可选关键字。

示例 1:用运算符检查条件的 LISP 程序

Lisp
;define value to 100
(setq val1 100)
 
;check the number is equal to 100
(if (= val1 100)
   (format t "equal to 100"))
    
(terpri)
 
;check the number is greater than to 50
(if (> val1 50)
   (format t "greater than 50"))
   
(terpri)
 
;check the number is less than to 150
(if (< val1 150)
   (format t "less than 150"))


Lisp
;define value to 230
(setq val1 230)
 
;check the number is equal to 100
(if (= val1 100)
   (format t "equal to 100"))
    
(terpri)
 
;check the number is greater than to 50
(if (> val1 50)
   (format t "greater than 50"))
   
(terpri)
 
;check the number is less than to 150
(if (< val1 250)
   (format t "less than 250"))


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")))


Lisp
;set number to 50
(setq number 50)
 
;condition check the given number is equal to 50
(when (= number 50)
 
;statement
  (format t "Equal to 50")
   )


Lisp
;set number to 50
(setq number 50)
 
;condition check the given number is equal to 50
(when (= number 50)
 
;statement
  (format t "Equal to 50")
   )
(terpri)
;set number to 150
(setq number 150)
 
;condition check the given number is greater than or equal to 50
(when (>= number 50)
 
;statement
  (format t "greater than or Equal to 50")
   )
(terpri) 
   ;set number to 10
(setq number 10)
 
;condition check the given number is less than or equal to 50
(when (<= number 50)
 
;statement
  (format t "less than or Equal to 50")
   )


Lisp
;define value to 2
(setq val1 2)
 
;define 5 cases from 1 to 5
(case val1
(1 (format t "you selected number 1"))
(2 (format t "you selected number 2"))
(3 (format t "you selected number 3"))
(4 (format t "you selected number 4"))
(5 (format t "you selected number 5"))
)


Lisp
;define value1 to 10
(setq val1 10)
 
;define value2 to 20
(setq val2 20)
 
;set input to 1
(setq input 1)
 
;define 4 cases to perform each arithmetic operation
(case input
 
;condition to perform addition
(1 (print (+ val1 val2)))
 
;condition to perform subtraction
(2 (print (- val1 val2)))
 
;condition to perform multiplication
(3 (print (* val1 val2)))
 
;condition to perform division
(4 (print (/ val1 val2)))
)


Lisp
;define value1 to 10
(setq val1 10)
 
;define value2 to 20
(setq val2 20)
 
;set input to 3
(setq input 3)
 
;define 4 cases to perform each arithmetic operation
(case input
 
;condition to perform addition
(1 (print (+ val1 val2)))
 
;condition to perform subtraction
(2 (print (- val1 val2)))
 
;condition to perform multiplication
(3 (print (* val1 val2)))
 
;condition to perform division
(4 (print (/ val1 val2)))
)


输出:

equal to 100
greater than 50
less than 150

示例 2:

语言

;define value to 230
(setq val1 230)
 
;check the number is equal to 100
(if (= val1 100)
   (format t "equal to 100"))
    
(terpri)
 
;check the number is greater than to 50
(if (> val1 50)
   (format t "greater than 50"))
   
(terpri)
 
;check the number is less than to 150
(if (< val1 250)
   (format t "less than 250"))

输出:

greater than 50
less than 250

条件声明

cond 是用于制定 n 个测试条件的决策语句。它将检查所有条件。

句法:

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

这里,

  • 条件指定了不同的条件——如果不满足条件 1,那么它会进入下一个条件 IE 条件,直到最后一个条件。
  • 语句指定基于条件完成的工作。

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

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

语言

;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")))

输出:

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

当语句

when 是用于指定决策的决策语句。它类似于条件语句。

句法:

(when (condition) (statements) )

在哪里,

  1. 条件是用于测试的测试语句
  2. 语句是取决于条件的动作

示例 1: LISP 程序检查数字是否等于 50

语言

;set number to 50
(setq number 50)
 
;condition check the given number is equal to 50
(when (= number 50)
 
;statement
  (format t "Equal to 50")
   )

输出:

Equal to 50

示例 2:使用运算符检查给定数字的 LISP 程序

语言

;set number to 50
(setq number 50)
 
;condition check the given number is equal to 50
(when (= number 50)
 
;statement
  (format t "Equal to 50")
   )
(terpri)
;set number to 150
(setq number 150)
 
;condition check the given number is greater than or equal to 50
(when (>= number 50)
 
;statement
  (format t "greater than or Equal to 50")
   )
(terpri) 
   ;set number to 10
(setq number 10)
 
;condition check the given number is less than or equal to 50
(when (<= number 50)
 
;statement
  (format t "less than or Equal to 50")
   )

输出:

Equal to 50
greater than or Equal to 50
less than or Equal to 50

案例声明

这用于一次检查多个测试条件,与 cond 不同,如果和何时允许多个条件。

语法

(case  (key_value)
((key1)   (statement 1 .............. statement n) )
((key2)   (statement 1 .............. statement n) )
................
((keyn)   (statement 1 .............. statement n) )

这里,

  1. key_value 是输入的数值
  2. 键是测试键中指定的特定条件的不同条件

示例: LISP 程序在给定数字时获取特定数字

语言

;define value to 2
(setq val1 2)
 
;define 5 cases from 1 to 5
(case val1
(1 (format t "you selected number 1"))
(2 (format t "you selected number 2"))
(3 (format t "you selected number 3"))
(4 (format t "you selected number 4"))
(5 (format t "you selected number 5"))
)

输出:

you selected number 2

示例:选择特定键时执行算术运算的 LISP 程序

语言

;define value1 to 10
(setq val1 10)
 
;define value2 to 20
(setq val2 20)
 
;set input to 1
(setq input 1)
 
;define 4 cases to perform each arithmetic operation
(case input
 
;condition to perform addition
(1 (print (+ val1 val2)))
 
;condition to perform subtraction
(2 (print (- val1 val2)))
 
;condition to perform multiplication
(3 (print (* val1 val2)))
 
;condition to perform division
(4 (print (/ val1 val2)))
)

输出:

30

将输入设置为 3

语言

;define value1 to 10
(setq val1 10)
 
;define value2 to 20
(setq val2 20)
 
;set input to 3
(setq input 3)
 
;define 4 cases to perform each arithmetic operation
(case input
 
;condition to perform addition
(1 (print (+ val1 val2)))
 
;condition to perform subtraction
(2 (print (- val1 val2)))
 
;condition to perform multiplication
(3 (print (* val1 val2)))
 
;condition to perform division
(4 (print (/ val1 val2)))
)

输出:

200