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

后端 未结 2 1893
南旧
南旧 2021-01-23 12:29

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

2条回答
  •  失恋的感觉
    2021-01-23 13:01

    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.

提交回复
热议问题