Lisp randomize and using two functions to pull from list into another

252 views Asked by At

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

My assignment is to ..

The prototype of the CANDY program is

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

In the following samples, we defines two CANDY-MACHINEs, one is gummy-bear machine that has 4 candies, the other is easter-egg machine that has 6 candies. The sample 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

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.

2

There are 2 answers

0
Rainer Joswig On BEST ANSWER

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

Finding wrong number of arguments:

Just compile your code:

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

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.

3
sheikh_anton On

Yes, you do have few argument in your call to cons here:

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

cons needs two arguments, when you giving it just one. So with what are you cosing your color?

Also indent your code properly, now it's really difficult to read.