Plaster
New
List
Login
text
default
anonymous
2021.12.13 17:39:46
;; Global variables are colloquially surrounded by *'s. ;; Initialize an empty variable called *DB* (defvar *db* nil) ;; Function that pushes an album to a "database". In this case, a global variable will store the information. (defun add-record (cd) (push cd *db*)) ;; Make output look better/ ;; This function loops over the contents of *db* and binds each element to cd, and then formats the output. ;; Format Takes at least two arguments: ;; 1. The stream, in this case *standard-output* ;; 2. The format string. ;; Format directives start with ~. ;; ~a = aesthetic directive, consumes one arg and outputs in human-readable form by removing colons (:) from keywords. ;; ~t = tabulating, ~10t tells FORMAT to emit enough spaces to move to the tenth column before processing the next ~a. ;; ~{ = Consumes a list as an argument, and loops over all items in that list. ;; ~% = Emit new line (defun dump-db () (dolist (cd *db*) (format t "~{~a:~10t~a~%~}~%" cd))) ;; We can technically format the albums inside *db* directly. ;; But it looks like hot garbage. (defun dumb-db-two () (format t "~{~{~a:~10t~a~%~}~%~}" *db*)) ;; Define a function with defun. (defun make-cd (title artist rating ripped) (list :title title :artist artist :rating rating :ripped ripped)) ;; Lets create a prompt that asks the user for album info: (defun prompt-read (prompt) (format *query-io* "~a: " prompt) (force-output *query-io*)) ;; Makes a more interactive prompt. (defun prompt-for-cd () (make-cd (prompt-read "Title") (prompt-read "Artist") (or (parse-integer (prompt-read "Rating?") :junk-allowed t) 0) (y-or-n-p "Ripped [y/n]: "))) ;; Finally, lets define a wrapper function that loops over prompt-for-cd. (defun add-cds () (loop (add-record (prompt-for-cd)) (if (not (y-or-n-p "Another? [y/n]: ")) (return))))
Raw
Annotate
Repaste
Edit