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.
coredump already answered your question.
You can write your code a bit shorter:
Finding wrong number of arguments:
Just compile your code:
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.