Lisp随机化并使用两个函数从列表拉入另一个

Lisp随机化并使用两个函数从列表拉入另一个

本文介绍了Lisp随机化并使用两个函数从列表拉入另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我是Lisp的新手,我已经从事此程序几天了,以了解Lisp并研究Lisp的某些部分,例如cons,cdr,let,funcall和其他一些部分.我正在尝试创建一种随机分配颜色的糖果机.我已经运行了无数次此代码,起初花了我一段时间才停止为随机函数获取错误,现在却说我对于generate-candy-supply中的缺点有很少的论点.有人对去哪里有任何建议以及解决方法吗?

Okay, so I am new to lisp and i have been working on this program for a couple days getting to know lisp and researching certain parts of lisp such as cons,cdr, let,funcall and some other ones. I am trying to create a candy machine that dispense colors randomly. I have ran this code numerous times and at first it took me a while to stop getting errors for the random function and now it is saying i have too few arguments for the cons in generate-candy-supply. Any one have any suggestions on where to go and any solution to this?

到目前为止,我的代码是...

so far my code is...

(defvar candy-color '(yellow red blue green pink orange))

(defun generate-candy-supply (size)
  (if (= 0 size)
    (cons (nth (random (length candy-color)) candy-color))
    (generate-candy-supply (- size 1))))

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

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

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

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

我的任务是去..

CANDY程序的原型是

The prototype of the CANDY program is

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

在以下示例中,我们定义了两台糖果机器,其中一台是橡皮熊有4个糖果的机器,另一个是有6个糖果的Easter-egg机器.这示例代码如下:

In the following samples, we defines two CANDY-MACHINEs, one is gummy-bearmachine that has 4 candies, the other is easter-egg machine that has 6 candies. Thesample code looks like this:

(defvar *gummy-bear*
        (candy-machine (generate-candy-supply 4)))
(defvar *easter-egg*
        (candy-machine (generate-candy-supply 6)))

该程序的示例运行如下:

Sample Run of this program looks like this:

[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

如果您可以帮助ID表示高度赞赏.我不是在寻找完整的答案,但是如果您可以向我指出正确的方向,那将会有所帮助.如果代码中也有错误,您可以指出这些错误.

if you could help id highly appreciate it. I am not looking for the full blown answer but if you can point me in the right direction that would be helpful. If there is errors in the code as well can you point those out.

推荐答案

coredump已经回答了您的问题.

coredump already answered your question.

您可以编写短一些的代码:

You can write your code a bit shorter:

(defvar *candy-color*
  #(yellow red blue green pink orange))    ; a vector

(defun generate-candy-supply (size)
  (loop repeat size
        collect (elt *candy-color*
                     (random (length *candy-color*)))))

(defun candy-machine (supply-of-candy)
  (lambda ()
    (pop supply-of-candy)))               ; use POP

发现错误的参数数量:

只需编译您的代码:

[2]> (compile ' generate-candy-supply)
WARNING: in GENERATE-CANDY-SUPPLY : CONS was called with 1 arguments, but it
         requires 2 arguments.

以上警告清楚地告诉您代码有什么问题.由于大多数Common Lisp实现都有一个或多个编译器,因此实际使用它们很有用.根据编译器的不同,他们会发现各种问题,例如错误的参数列表,未使用的变量,未声明的变量等等.

Above warning clearly tells you what is wrong with the code. Since most Common Lisp implementations have one or more compilers, it's useful to actually use them. Depending on the compiler, they can find various problems like wrong argument lists, unused variables, undeclared variables and more.

这篇关于Lisp随机化并使用两个函数从列表拉入另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:06