(defun card-value (card) ;checking for face values (case card (11 10) ;if jack, the value is 10 (12 10) ;if queen, the value is 10 (13 10) ;if king, the value is 10 (1 11) ;if jack, set the default to 11. We will fix this later. (otherwise card))) ;otherwise, the value stays the same. (defun countaces (total acecount) ;updating when the ace is making the score higher than 21 (if (and (> total 21) (> acecount 0)) ;if the total is over 21 and there is an ace... (countaces (- total 10)(- acecount 1)) ;then subtract 10 from total, so it is no longer an ace... repeat recursively total)) (defun adding-points (hand total acecount) ;let's find the total points! (if (null hand) ;checking if null (countaces total acecount) ;calculating total (adding-points (cdr hand) (+ total (card-value (car hand))) (+ acecount (if (= (car hand) 1) 1 0))))) (defun blackjack (hand) (adding-points hand 0 0)) ;set the default for total and acecount to 0 (format t "total: ~a" (blackjack '(1 12 13)))
Write, Run & Share Common Lisp code online using OneCompiler's Common Lisp online compiler for free. It's one of the robust, feature-rich online compilers for Common Lisp language, running the latest Common Lisp version 5.3. Getting started with the OneCompiler's Common Lisp editor is easy and fast. The editor shows sample boilerplate code when you choose language as Common Lisp and start coding.
OneCompiler's Common Lisp online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Common Lisp program which takes name as input and prints hello message with your name.
(setq name (read))
(princ "Hello ")
(write name)
Common Lisp is a generic language suitable for a wide range of industry applications. It is often referred as Programmable programming language because of it's high extensibility, machine independence, extensive control structures, dynamic updation of programs etc.
Common LISP was invented by John McCarthy in 1958 and was first implemenyted by Steve Russell on an IBM 704 computer.
defvar
keyword and these variables will be in effect until a new value is assigned.(defvar x 10)
(write x)
let
and prog
are used to declare local variables.(let ((var1 value1) (var2 value2).. (varn valuen))<expressions>)
setq
(setq a 10)
This is the simplest looping mechanism in LISP. This allows the execute the set of statements repeatedly until a return statement is encountered.
(loop (s-expressions))
For loop is used to iterate a set of statements based on a condition.
(loop for loop-variable in <a list>
do (action)
)
Do is also used to iterate a set of statements and then check the condition
(do ((var1 val1 updated-val1)
(var2 val2 updated-val2)
(var3 val3 updated-val3)
...)
(test return-value)
(s-expressions)
)
Dotimes is used to iterate for fixed number of iterations.
(dotimes (n val)
statements