📜  在 LISP 中构建

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

在 LISP 中构建

在本文中,我们将讨论 LISP 中的Do Construct。 Do 构造用于以结构化格式执行迭代。

语法

(do ((variable1    value1   new updated value 1)
    (variable2   value2   new updated value 2)
-------------------------------
-------------------------------
    (variable n   value n    new updated value n)
 ...)
 (test statements)  
)

这里,

  1. 变量是输入值。
  2. 新的更新值是根据值中给定的条件更新的值。
  3. 测试语句将在最后执行。

示例 1 :LISP 程序获取值递增和递减显示,直到给定条件

Lisp
;define value 1 to 0
;define value 2 to 30
(do ((val1 0 (+ 2 val1))
   (val2 24 ( - val2 2)))
    ;display both values until value1 is equal to  value2
    ;by incrementing value1 by 2
    ;by decrementing value2 by 2
   ((= val1 val2)(- val2 val2))
    ;display
   (format t "~% value 1 = ~d  and  value 2  = ~d" val1 val2)
)


Lisp
;define value 1 to 0
;define value 2 to 30
(do ((val1 0 (+ 2 val1))
   (val2 24 ( - val2 2)))
    
    ;display both values until value1 is greater than  value2
    ;by incrementing value1 by 2
    ;by decrementing value2 by 2
   ((> val1 val2)(- val2 val2))
    ;display
   (format t "~% value 1 = ~d  and  value 2  = ~d" val1 val2)
)


输出:

value 1 = 0  and  value 2  = 24
value 1 = 2  and  value 2  = 22
value 1 = 4  and  value 2  = 20
value 1 = 6  and  value 2  = 18
value 1 = 8  and  value 2  = 16
value 1 = 10  and  value 2  = 14

示例 2: LISP 程序通过执行递增和递减操作来显示所有值 1 大于值 2

语言

;define value 1 to 0
;define value 2 to 30
(do ((val1 0 (+ 2 val1))
   (val2 24 ( - val2 2)))
    
    ;display both values until value1 is greater than  value2
    ;by incrementing value1 by 2
    ;by decrementing value2 by 2
   ((> val1 val2)(- val2 val2))
    ;display
   (format t "~% value 1 = ~d  and  value 2  = ~d" val1 val2)
)

输出:

value 1 = 0  and  value 2  = 24
value 1 = 2  and  value 2  = 22
value 1 = 4  and  value 2  = 20
value 1 = 6  and  value 2  = 18
value 1 = 8  and  value 2  = 16
value 1 = 10  and  value 2  = 14
value 1 = 12  and  value 2  = 12