~/.guile
guile 的 load path 是由 %load-path 变量控制的。把路径加入这个变量就行了。
或者设置环境变量 GUILE_LOAD_PATH.
*features* 变量。
能。它在 guile 的 ice-9 模块里。
(use-syntax (ice-9 syncase))
之后就能使用 syntax-case 了。
使用 (ice-9 debug) 模块就行了。(debug) 就可以进入 debugger.
可以使用 trace untrace trace-stack untrace-stack 来跟踪函数。
如果想要更方便的设置断点,可 以使用 guile-debugger。可以在这里下载:
http://www.ossau.uklinux.net/guile/index.html
在程序里使用
(use-modules (ossau breakpoints))
就能设置断点了。
在 Guile 里使用 goops 模块就行了。
(use-modules (oop goops))
比如,我们来重载一个加法操作符 "+":
(define-method (+ (x <string>) (y <string>)) (string-append x y)) (+ 1 2) (+ "abc" "de")
(ice-9 time)
(use-modules (ice-9 time)) (time (sleep 3))
(use-modules (ice-9 expect))
使用 format 就行了。先加载 format 模块。
(use-modules (ice-9 format))
然后使用 format
format destination format-string args ...
destination 是输出的端口。如果是一个 port 就把格式化好的内容 送到那个端口。如果是 #t 就输出到 current output port。如果是 就输出到一个 string。如果是一个数就输出到 stderr.
格式化串里常用字符的含义:
(use-modules (ice-9 readline)) (activate-readline)
如果你不想用 OOP 但是想根据参数个数来重载函数名,可以用 case-lambda。它在 SRFI-16 里。你可以这样使用:
(use-modules (srfi srfi-16)) (define foo (case-lambda ((x) #t) ((x y) (+ x y)) (z (apply * z)))) (foo 'bar) (foo 2 4) (foo 3 3 3)
SRFI-13 有很多方便的操作 string 的函数。 比如:
string-any pred s [start end] string-every pred s [start end]
用于判断字符串里的字符是否满足 pred 设定的条件。
string-tabulate proc len
制造一个长度 len 的 string, 对每一个位置使用 proc 来初始化。
string-join ls [delimiter grammar]
把 list ls 里的字符组合成一个字符串。使用分解符 delimiter, 使用语法 grammar。grammar 可以是 infix, string-infix, suffix, 或者 prefix.
string-copy str [start end]
拷贝子字符串。
substring/shared str start [end]
也是拷贝。但是共享空间。
string-copy! target tstart s [start end]
把 s 里 [start, end) 之间的部分拷贝到 target 里从 tstart 开 始的区域。target 和 s 可以是同一个 string.
string-fill! str chr [start end]
string-index s char_pred [start end]
在 s 里寻找 char-pred, 如果 char-pred 是一个字符。否则在 s 里寻找满足 char-pred 函数的位置。
string-contains s1 s2 [start1 end1 start2 end2] string-contains-ci s1 s2 [start1 end1 start2 end2]
在 s1 里寻找 s2.
string-replace s1 s2 [start1 end1 start2 end2]
把 s1 里的一部分换成 s2 里的一部分。
string-filter s char_pred [start end]
把 s 里的字符通过 char_pred 过滤。
string-delete s char_pred [start end]
作用正好跟 string-filter 相反。
比如:
(define (p1 x) (char-upper-case? x)) (string-filter "kiHck!shAit*hLaLaldsjfOha" p1)
结果是 "HALLO".
string-tokenize s [token_char start end]
把一个string切分成几个,用 token_char 作为分割。如果没有 token_char 就用空白作为分割。比如:
(string-tokenize "ha kick flip")
结果是 ("ha" "kick" "flip").
SRFI-1 里有很多函数用来操作 list.
SRFI-6 提供了 string port 的支持。
open-input-string, open-output-string and get-output-string
SRFI-9 提供 define-record-type. 这跟 Scheme 48 的 record一样。
比如:
guile> (use-modules (srfi srfi-9)) guile> (define-record-type :foo (make-foo x) foo? (x get-x) (y get-y set-y!)) guile> (define f (make-foo 1)) guile> f #<:foo x: 1 y: #f> guile> (get-x f) 1 guile> (set-y! f 2) 2 guile> (get-y f) 2 guile> f #<:foo x: 1 y: 2> guile> (foo? f) #t guile> (foo? 1) #f
SRFI-11 提供了 let-values.
(use-modules (srfi srfi-11)) (let-values (((x y) (values 1 2)) ((z f) (values 3 4))) (+ x y z f))
使用 (ice-9 pretty-print) 就行。比如:
(use-modules (ice-9 pretty-print)) (pretty-print '(define (foo) (lambda (x) (cond ((zero? x) #t) ((negative? x) -x) (else (if (= x 1) 2 (* x x x)))))))
结果是:
(lambda (x) (cond ((zero? x) #t) ((negative? x) -x) (else (if (= x 1) 2 (* x x x))))))
(use-modules (ice-9 history)) guile> 1 $1 = 1 guile> (+ $1 $1) $2 = 2 guile> (* $2 $2) $3 = 4
(debug-enable 'backtrace)
Scheme Procedure: read-enable option-symbol Scheme Procedure: print-enable option-symbol Scheme Procedure: debug-enable option-symbol Scheme Procedure: trap-enable option-symbol Scheme Procedure: read-disable option-symbol Scheme Procedure: print-disable option-symbol Scheme Procedure: debug-disable option-symbol Scheme Procedure: trap-disable option-symbol syntax: read-set! option-symbol value syntax: print-set! option-symbol value syntax: debug-set! option-symbol value syntax: trap-set! option-symbol value
keywords #f Style of keyword recognition: #f or 'prefix case-insensitive no Convert symbols to lower case. positions yes Record positions of source code expressions. copy no Copy source code expressions.
stack 22000 Size of thread stacks (in machine words).
source no Print closures with source. closure-hook #f Hook for printing closures.
stack 20000 Stack size limit (0 = no check). debug yes Use the debugging evaluator. backtrace no Show backtrace on error. depth 20 Maximal length of printed backtrace. maxdepth 1000 Maximal number of stored backtrace frames. frames 3 Maximum number of tail-recursive frames in backtrace. indent 10 Maximal indentation in backtrace. backwards no Display backtrace in anti-chronological order. procnames yes Record procedure names at definition. trace no *Trace mode. breakpoints no *Check for breakpoints. cheap yes *Flyweight representation of the stack at traps.
使用 (ice-9 popen) module:
(use-modules (ice-9 popen)) (define p (open-input-pipe "ls -l")) (read-line p)
试试这些:
(define a '(2 3 45 5 6)) (define b '(83 3 59 18 38)) (sorted? a <) (define as (sort a <)) (define bs (sort b <)) (merge as bs <) (sort! a <) (stable-sort b <) (define v1 '#(23 4 5 8 34 83 23 18 35)) (sort v1 <) (restricted-vector-sort! v1 < 4 7)
copy-tree.
object->string
(object->string fact1)