lisp 程式碼示例
非原創,抄來的:
!article/FuncProgram/2185
剛學lisp不久:)有些地方還不是很清楚,所以用了比較笨的辦法,還請大家多多指教
//bow
;; s 開始狀態
;; 結束狀態集
;; rules 規則集
;; delta 轉移函式
;; search.lisp
;; 22 March 2004
(defpackage search-base
(:export BFS DFS)
(:use common-lisp))
(in-package search-base)
(defvar finish-status '()
"The finished status set")
(defvar rules-set '())
(defvar expand-func #'(lambda () ()))
(defvar update-open-lst #'(lambda () ()))
(defun search-init (K rules delta lstop)
"initialize the global values used by search base"
(setq finish-status K)
(setq rules-set rules)
(setq expand-func delta)
(setq update-open-lst lstop)
t)
(defun search-start (open closed)
(let ((q (first open))
(seq (rest open)))
(cond ((null open) nil)
((find q finish-status :test 'equal) (list seq (cons q closed)))
(t (search-start (funcall
update-open-lst
seq
(expand-vertex q rules-set expand-func (cons q closed))
)
(cons q closed))))))
;; s 開始狀態
;; 結束狀態集
;; rules 規則集
;; delta 轉移函式
;; 寬度優先
(defun BFS (s K rules delta)
"Breadth first search"
(let ((lstop #'(lambda (lst1 lst2)
(append lst1 lst2)))
)
(search-init K rules delta lstop)
(search-start (list s) nil)))
;; 深度優先
(defun DFS (s K rules delta)
"Deep first search"
(let ((lstop #'(lambda (lst1 lst2)
(append lst2 lst1)))
)
(search-init K rules delta lstop)
(search-start (list s) nil)))
(defun expand-vertex (vertex rules delta closed)
(let ((lst (mapcar
#'(lambda (r)
(let ((v (funcall delta vertex r))
)
(cond ((find v closed :test 'equal) nil)
(t v))))
rules))
)
(remove nil lst)))
(defun test-delta (v r)
(+ v r))
;; 下面是用分水問題來作測試
;; water.lisp
(load "search")
(defvar rules '(
(0 1)
(0 2)
(1 0)
(1 2)
(2 0)
(2 1)
))
(defvar limits '(8 5 3))
(defvar start '(8 0 0))
(defvar finish '(
(4 4 0)
))
(defun index(lst idx)
(cond ((equal idx 0) (car lst))
(t (index (cdr lst) (1- idx)))))
(defun setidx-base(lst idx val ret)
(let ((first-lst (car lst))
(rest-lst (cdr lst))
)
(cond ((eql 0 idx) (append ret (cons val rest-lst)))
(t (setidx-base
rest-lst
(1- idx)
val
(append ret (list first-lst)))))))
(defun setidx(lst idx val)
(setidx-base lst idx val nil))
(defun pour-water (q src dst)
(let* ((src-wt (index q src))
(dst-wt (index q dst))
(empty (- (index limits dst) dst-wt))
)
(cond ((equal 0 src-wt) q)
((> src-wt empty)
(let* ((new (setidx q src (- src-wt empty)))
(new (setidx new dst (index limits dst)))
)
new))
(t (let* ((new (setidx q src 0))
(new (setidx new dst (+ src-wt dst-wt)))
)
new)))))
(defun pour (q r)
(pour-water q (first r) (second r)))
(print (search-base:DFS start finish rules 'pour))
(quit)
--
(defun power-set (set)
(if (null set) '(())
(let ((pset-of-rest (power-set (cdr set))))
(append
(mapcar #'(lambda (subset) (cons (car set) subset))
pset-of-rest) pset-of-rest))))
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-721770/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 排序程式碼示例排序
- RabbitMQ 程式碼示例MQ
- Java NIO 程式碼示例Java
- java SPI 程式碼示例Java
- 【轉】Lisp 已死,Lisp 萬歲!Lisp
- Lisp 永遠成不了程式設計主流語言 vs Lisp 神話之路Lisp程式設計
- js程式碼與html程式碼分離示例JSHTML
- Kafka 1.0.0 d程式碼示例Kafka
- 靜態代理程式碼示例
- 視窗程式框架示例程式碼框架
- RSA加密解密示例程式碼加密解密
- pytorch程式碼示例筆記 -- AutogradPyTorch筆記
- 哈夫曼編碼 —— Lisp 與 Python 實現LispPython
- sitemap 檔案填充示例程式碼
- Opencv及常用方法示例程式碼OpenCV
- Lisp 整體思想Lisp
- Android HAL 層框架分析以及程式碼示例Android框架
- 支付寶程式碼示例結構說明
- Android Studio 藍芽 示例程式碼(轉)Android藍芽
- Android 相對佈局RelativeLayout 程式碼示例Android
- 影片聊天原始碼,同步、非同步示例程式碼分析原始碼非同步
- python爬蟲之多執行緒、多程式+程式碼示例Python爬蟲執行緒
- Lisp求平方根Lisp
- lisp 判斷時間Lisp
- 淺談JavaScript程式碼預解析 + 示例詳解JavaScript
- Netty ServerBootstrap 繫結多個埠(程式碼示例)NettyServerboot
- iOS 中常見的幾種鎖-程式碼示例iOS
- 20個Python random模組的程式碼示例Pythonrandom
- Spring 註解學習 詳細程式碼示例Spring
- BQspringboot整合mongodb?changestream的示例程式碼mmuSpring BootMongoDB
- php原聲實現分頁程式碼示例PHP
- 答答租車系統規範程式碼示例
- mybatis中註解對映SQL示例程式碼MyBatisSQL
- 物件導向設計介紹和程式碼示例物件
- 常用設計模式之.Net示例程式碼合集設計模式
- Avalonia 後臺程式碼簡單播放動畫示例動畫
- pytorch深度學習分類程式碼簡單示例PyTorch深度學習
- Redis實現排名功能的示例程式碼CRKCRedis
- APP訊息推送 極光推送 示例程式碼APP