common lisp 学习第二天 简单数据库操作
生活随笔
收集整理的這篇文章主要介紹了
common lisp 学习第二天 简单数据库操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//簡單數據操作 單一記錄 > (defun make-cd (title artist rating ripped) (list :title title :artist artist :rating rating :ripped ripped)) make-cd > (make-cd "Roses" "Kathy Mattea" 7 t) (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)
定義全局變量(數據庫) > (defvar *db* nil) 添加記錄函數 > (defun add-record (cd) (push cd *db*)) 調用 CL-USER> (add-record (make-cd "Roses" "Kathy Mattea" 7 t))? ((:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)) CL-USER> (add-record (make-cd "Fly" "Kathy Mattea" 8 t)) ((:TITLE "Fly" :ARTIST "Kathy Mattea" :RATING 8 :RIPPED T)? (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)) CL-USER> (add-record (make-cd "Home" "Kathy Mattea" 9 t)) ((:TITLE "Home" :ARTIST "Kathy Mattea" :RATING 9 :RIPPED T)? (:TITLE "Fly" :ARTIST "Kathy Mattea" :RATING 8 :RIPPED T)? (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)) 查看數據庫內容 > *db* 格式化顯示 > (defun dump-db () (dolist (cd *db*) (format t "~{~a: ~10t~a~%~}~%" cd))) > (dump-db) TITLE: ? ?Home ARTIST: ? Kathy Mattea RATING: ? 9 RIPPED: ? T
TITLE: ? ?Fly
//format?destination control-string args destination 目標 t為標準輸出流 control-string 格式字符串 ~a 美化指令,去掉雙引號、前導冒號。 ~t 制表,~10t產生10個空格。
~% 換行
//讀取輸入 (defun prompt-read (prompt) (format *query-io* "~a: " prompt) (force-output *query-io*) (read-line *query-io*))
//轉換整數 輸入其他字符報錯 (parse-integer (prompt-read "Rating")) 允許輸入其他字符 (parse-integer (prompt-read "Rating") :junk-allowed t) 找不到整數默認為0 (or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
//限制輸入y/n (y-or-n-p "Ripped [y/n]") //輸入一條CD (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]")))
定義全局變量(數據庫) > (defvar *db* nil) 添加記錄函數 > (defun add-record (cd) (push cd *db*)) 調用 CL-USER> (add-record (make-cd "Roses" "Kathy Mattea" 7 t))? ((:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)) CL-USER> (add-record (make-cd "Fly" "Kathy Mattea" 8 t)) ((:TITLE "Fly" :ARTIST "Kathy Mattea" :RATING 8 :RIPPED T)? (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)) CL-USER> (add-record (make-cd "Home" "Kathy Mattea" 9 t)) ((:TITLE "Home" :ARTIST "Kathy Mattea" :RATING 9 :RIPPED T)? (:TITLE "Fly" :ARTIST "Kathy Mattea" :RATING 8 :RIPPED T)? (:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)) 查看數據庫內容 > *db* 格式化顯示 > (defun dump-db () (dolist (cd *db*) (format t "~{~a: ~10t~a~%~}~%" cd))) > (dump-db) TITLE: ? ?Home ARTIST: ? Kathy Mattea RATING: ? 9 RIPPED: ? T
TITLE: ? ?Fly
//format?destination control-string args destination 目標 t為標準輸出流 control-string 格式字符串 ~a 美化指令,去掉雙引號、前導冒號。 ~t 制表,~10t產生10個空格。
~% 換行
//讀取輸入 (defun prompt-read (prompt) (format *query-io* "~a: " prompt) (force-output *query-io*) (read-line *query-io*))
//轉換整數 輸入其他字符報錯 (parse-integer (prompt-read "Rating")) 允許輸入其他字符 (parse-integer (prompt-read "Rating") :junk-allowed t) 找不到整數默認為0 (or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
//限制輸入y/n (y-or-n-p "Ripped [y/n]") //輸入一條CD (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]")))
總結
以上是生活随笔為你收集整理的common lisp 学习第二天 简单数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何完整离线保存网页,包括网页完整特效?
- 下一篇: 烽火2640路由器命令行手册-12-IB