我需要一种方法来获取正在运行的脚本的路径(包含源文件的目录),但是
(current-directory)
Run Code Online (Sandbox Code Playgroud)
从不指向那里(在这种情况下是外部驱动器),而是指向某个预定义的位置.
我创建了一个文件来尝试所有'find-system-path',但它们都不是正在运行的文件!Racket文档没有帮助.
#lang web-server/insta
(define (start request)
(local [{define (build-ul items)
`(ul ,@(map itemize items))}
{define (itemize item)
`(li ,(some-system-path->string (find-system-path item)))}]
(response/xexpr
`(html
(head (title "Directories"))
(body (h1 ,"Some Paths")
(p ,(build-ul special-paths)))))))
(define special-paths (list 'home-dir
'pref-dir
'pref-file
'temp-dir
'init-dir
'init-file
;'links-file ; not available for Linux
'addon-dir
'doc-dir
'desk-dir
'sys-dir
'exec-file
'run-file
'collects-dir
'orig-dir))
Run Code Online (Sandbox Code Playgroud)
目的是本地Web服务器应用程序(音乐服务器),它将修改包含源文件的目录下的子目录.我将在USB记忆棒上携带应用程序,因此我需要能够找到自己的目录,因为我在安装了Racket的机器和操作系统之间进行了该目录.
此宏的目的是创建一个宏,该宏为访问关联列表的某个键提供名称.
(defmacro generate-accessor (key-symbol prefix)
(let ((mac-name
(intern (string-upcase (concatenate 'string
prefix "-"
(string key-symbol))))))
`(defmacro ,mac-name (alis) `(assoc ,',key-symbol ,alis))))
Run Code Online (Sandbox Code Playgroud)
所以,当我尝试它 -
CL-USER> (generate-accessor 'a "alist")
; ERROR> 'A cannot be coerced to a string.
Run Code Online (Sandbox Code Playgroud)
但是...
CL-USER> (string 'a)
; RESULT> "A"
Run Code Online (Sandbox Code Playgroud)
所以我再次尝试使用SYMBOL-NAME将符号强制转换为字符串
(defmacro generate-accessor (key-symbol prefix)
(let ((mac-name
(intern (string-upcase (concatenate 'string
prefix "-"
(symbol-name key-symbol))))))
`(defmacro ,mac-name (alis) `(assoc ,',key-symbol ,alis))))
Run Code Online (Sandbox Code Playgroud)
这次我试试 -
CL-USER> (generate-accessor 'a "alist")
; ERROR> The value 'A is not of type SYMBOL. …Run Code Online (Sandbox Code Playgroud)