gpt4 book ai didi

LISP 帮助!糖果机分配器

转载 作者:太空宇宙 更新时间:2023-11-03 18:59:02 26 4
gpt4 key购买 nike

<分区>

大家好,我是新来的,我想知道你们是否可以给我任何帮助。我正在创建一个随机分配颜色的糖果机。我已经运行了这段代码,它为 (get-candy gummy-bear) 运行了 7 次,但它应该运行 4 次直到 nil .

所以这是我的代码:

 ;;; function get-candy
(defun get-candy (machine)
(funcall machine))

;;; variable colors
(defvar *colors* '(red blue green brown yellow purple))

;;; function generate-candy-supply
(defun generate-candy-supply (num)
(if (= 0 num)
(cons (nth (+ 1 (random (- (length *colors*) 1))) *colors*) *colors* )
(generate-candy-supply (- num 1))))


;;; function candy-machine
(defun candy-machine (candy)
(function
(lambda ()
(prog1 (car candy)
(setq candy (cdr candy))))))


;;; variable gummy-bear
(defvar *gummy-bear*
(candy-machine (generate-candy-supply 4)))

;;; variable easter-egg
(defvar *easter-egg*
(candy-machine (generate-candy-supply 6)))

样本运行应如下所示:

[1]> (load 'candy.lisp)
;; Loading file candy.lisp ...
;; Loaded file candy.lisp
T
[2]> (get-candy *gummy-bear*)
BLUE
[3]> (get-candy *gummy-bear*)
BROWN
[4]> (get-candy *gummy-bear*)
YELLOW
[5]> (get-candy *gummy-bear*)
YELLOW
[6]> (get-candy *gummy-bear*)
NIL
[7]> (get-candy *easter-egg*)
BLUE
[8]> (get-candy *easter-egg*)
BROWN
[9]> (get-candy *easter-egg*)
GREEN
[10]> (get-candy *easter-egg*)
BROWN
[11]> (get-candy *easter-egg*)
YELLOW
[12]> (get-candy *easter-egg*)
BLUE
[13]> (get-candy *easter-egg*)
NIL

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com