(define (print item . rest)
(let ((all-item (cons item rest)))
(for-each
(lambda (item) (display item) (display " "))
all-item))
(newline))
(use-modules (ice-9 popen) (srfi srfi-1) (srfi srfi-13))
(define (drain-output port)
(let loop ((chars '())
(next (read-char port)))
(if (eof-object? next)
(list->string (reverse! (cdr chars)))
(loop (cons next chars)
(read-char port)))))
(define (qx pipeline)
(let* ((pipe (open-input-pipe pipeline))
(output (drain-output pipe)))
(close-pipe pipe)
output))
(define string "\\n") (define string "\n") (define string "Jon \"Maddog\" Orwant") (define string "Jon 'Maddog' Orwant")
(define a "This is a multiline here document
terminated by a closing double quote")
(substring str start end)
(substring str start)
(substring-move-right! str start end newstring newstart)
(substring-move-left! str start end newstring newstart)
(string-ref str pos)
(string-set! str pos char)
(string-fill! str char)
(substring-fill! str start end char)
(define s "This is what you have")
(define first (substring s 0 1)) (define start (substring s 5 7)) (define rest (substring s 13)) (define last (substring s (1- (string-length s)))) (define end (substring s (- (string-length s) 4))) (define piece (let ((len (string-length s)))
(substring s (- len 8) (- len 5))))
(use-modules (srfi srfi-13))
(define s "This is what you have")
(define first (string-take s 1)) (define start (xsubstring s 5 7)) (define rest (xsubstring s 13 -1)) (define last (string-take-right s 1)) (define end (string-take-right s 4)) (define piece (xsubstring s -8 -5))
(set! s (string-replace s "wasn't" 5 7))
(set! s (string-replace s "ondrous" 13 25))
(set! s (string-take-right s (1- (string-length s))))
(set! s (string-take s 9))
(define a (or b c))
(define a (if (defined? b) b c))
(define a (or (and (defined? b) b) c))
(let ((a b) (b a))
)
(let ((alpha beta) (beta production) (production alpha))
)
(define num (char->integer char))
(define char (integer->char num))
(use-modules (srfi srfi-13))
(let ((str "sample"))
(display (string-join
(map number->string
(map char->integer (string->list str))) " "))
(newline))
(let ((lst '(115 97 109 112 108 101)))
(display (list->string (map integer->char lst)))
(newline))
(letrec ((next (lambda (c) (integer->char (1+ (char->integer c))))))
(let* ((hal "HAL")
(ibm (list->string (map next (string->list hal)))))
(display ibm)
(newline)))
(map proc
(string->list str))
(use-modules (srfi srfi-1))
(format #t "unique chars are: ~A\n"
(apply string (sort (delete-duplicates
(string->list "an apple a day")) char<?)))
(let* ((str "an apple a day")
(sum (apply + (map char->integer (string->list str)))))
(format #t "sum is ~A\n" sum))
(use-modules (srfi srfi-13))
(let* ((str "an apple a day")
(sum (string-fold (lambda (c acc) (+ acc (char->integer c)))
0 str)))
(format #t "sum is ~A\n" sum))
#!/usr/local/bin/guile -s
!#
(use-modules (srfi srfi-13))
(define (checksum p)
(let loop ((line (read-line p 'concat)) (sum 0))
(if (eof-object? line)
(format #t "~A ~A\n" sum (port-filename p))
(let ((line-sum (string-fold (lambda (c acc)
(+ acc (char->integer c)))
0 line)))
(loop (read-line p 'concat) (modulo (+ sum line-sum)
(1- (expt 2 16))))))))
(let ((args (cdr (command-line))))
(if (null? args)
(checksum (current-input-port))
(for-each (lambda (f) (call-with-input-file f checksum)) args)))
#!/usr/local/bin/guile -s
!#
(use-modules (ice-9 regex) (srfi srfi-2) (srfi srfi-13))
(define args (cdr (command-line)))
(define delay 1)
(and-let* ((p (pair? args))
(m (string-match "^-([0-9]+)$" (car args))))
(set! delay (string->number (match:substring m 1)))
(set! args (cdr args)))
(define (slowcat p)
(let loop ((line (read-line p 'concat)))
(cond ((not (eof-object? line))
(string-for-each
(lambda (c) (display c) (usleep (* 5 delay))) line)
(loop (read-line p 'concat))))))
(if (null? args)
(slowcat (current-input-port))
(for-each (lambda (f) (call-with-input-file f slowcat)) args))
(define revbytes (list->string (reverse (string->list str))))
(use-modules (srfi srfi-13))
(define revbytes (string-reverse str))
(string-reverse! str)
(define revwords (string-join (reverse (string-tokenize str)) " "))
(with-input-from-file "/usr/share/dict/words"
(lambda ()
(do ((word (read-line) (read-line)))
((eof-object? word))
(if (and (> (string-length word) 5)
(string=? word (string-reverse word)))
(write-line word)))))
(regexp-substitute/global
#f "([^\t]*)(\t+)" str
(lambda (m)
(let* ((pre-string (match:substring m 1))
(pre-len (string-length pre-string))
(match-len (- (match:end m 2) (match:start m 2))))
(string-append
pre-string
(make-string
(- (* match-len 8)
(modulo pre-len 8))
#\space))))
'post)
(define (varsubst str)
(regexp-substitute/global #f "\\$(\\w+)" str
'pre (lambda (m) (eval (string->symbol (match:substring m 1))
(current-module)))
'post))
(define (safe-varsubst str)
(regexp-substitute/global #f "\\$(\\w+)" str
'pre (lambda (m)
(catch #t
(lambda () (eval (string->symbol (match:substring m 1))
(current-module)))
(lambda args
(format #f "[NO VARIABLE: ~A]" (match:substring m 1)))))
'post))
(define (interpolate str)
(regexp-substitute/global #f "\\${([^{}]+)}" str
'pre (lambda (m) (eval-string (match:substring m 1))) 'post))
(use-modules (srfi srfi-13))
(string-upcase "bo beep") (string-downcase "JOHN") (string-titlecase "bo") (string-titlecase "JOHN")
(string-titlecase "thIS is a loNG liNE")
#!/usr/local/bin/guile -s
!#
(use-modules (srfi srfi-13))
(seed->random-state (current-time))
(define (randcap p)
(let loop ((line (read-line p 'concat)))
(cond ((not (eof-object? line))
(display (string-map (lambda (c)
(if (= (random 5) 0)
(char-upcase c)
(char-downcase c)))
line))
(loop (read-line p 'concat))))))
(let ((args (cdr (command-line))))
(if (null? args)
(randcap (current-input-port))
(for-each (lambda (f) (call-with-input-file f randcap)) args)))
(format #f "I have ~A guanacos." n)
(define var "
your text
goes here")
(use-modules (ice-9 regexp))
(set! var (regexp-substitute/global #f "\n +" var 'pre "\n" 'post))
(use-modules (srfi srfi-13))
(set! var (string-join (map string-trim (string-tokenize var #\newline)) "\n"))
(use-modules (ice-9 regexp) (srfi srfi-13) (srfi srfi-14))
(define (dequote str)
(let* ((str (if (char=? (string-ref str 0) #\newline)
(substring str 1) str))
(lines (string-tokenize str #\newline))
(rx (let loop ((leader (car lines)) (lst (cdr lines)))
(cond ((string= leader "")
(let ((pos (or (string-skip (car lines)
char-set:whitespace) 0)))
(make-regexp (format #f "^[ \\t]{1,~A}" pos)
regexp/newline)))
((null? lst)
(make-regexp (string-append "^[ \\t]*"
(regexp-quote leader))
regexp/newline))
(else
(let ((pos (or (string-prefix-length leader (car lst))
0)))
(loop (substring leader 0 pos) (cdr lst))))))))
(regexp-substitute/global #f rx str 'pre 'post)))
(use-modules (srfi srfi-13))
(define text "Folding and splicing is the work of an editor,
not a mere collection of silicon
and
mobile electrons!")
(define (wrap str max-col)
(let* ((words (string-tokenize str))
(all '())
(first (car words))
(col (string-length first))
(line (list first)))
(for-each
(lambda (x)
(let* ((len (string-length x))
(new-col (+ col len 1)))
(cond ((> new-col max-col)
(set! all (cons (string-join (reverse! line) " ") all))
(set! line (list x))
(set! col len))
(else
(set! line (cons x line))
(set! col new-col)))))
(cdr words))
(set! all (cons (string-join (reverse! line) " ") all))
(string-join (reverse! all) "\n")))
(display (wrap text 20))
(define str "Mom said, \"Don't do that.\"")
(set! str (regexp-substitute/global #f "['\"]" str 'pre "\\"
match:substring 'post))
(set! str (regexp-substitute/global #f "[^A-Z]" str 'pre "\\"
match:substring 'post))
(set! str (string-append "this " (regexp-substitute/global
#f "\W" "is a test!" 'pre "\\"
match:substring 'post)))
(use-modules (srfi srfi-13))
(define str " space ")
(string-trim str) (string-trim-right str) (string-trim-both str)
(use-modules (srfi srfi-2) (srfi srfi-13) (ice-9 format))
(define parse-csv
(let* ((csv-match (string-join '("\"([^\"\\\\]*(\\\\.[^\"\\\\]*)*)\",?"
"([^,]+),?"
",")
"|"))
(csv-rx (make-regexp csv-match)))
(lambda (text)
(let ((start 0)
(result '()))
(let loop ((start 0))
(and-let* ((m (regexp-exec csv-rx text start)))
(set! result (cons (or (match:substring m 1)
(match:substring m 3))
result))
(loop (match:end m))))
(reverse result)))))
(define line "XYZZY,\"\",\"O'Reilly, Inc\",\"Wall,
Larry\",\"a \\\"glug\\\" bit,\",5,\"Error, Core Dumped\"")
(do ((i 0 (1+ i))
(fields (parse-csv line) (cdr fields)))
((null? fields))
(format #t "~D : ~A\n" i (car fields)))
(use-modules (srfi srfi-13) (srfi srfi-14))
(define soundex
(letrec ((chars "AEIOUYBFPVCGJKQSXZDTLMNR")
(nums "000000111122222222334556")
(skipchars (string->char-set "HW"))
(trans (lambda (c)
(let ((i (string-index chars c)))
(if i (string-ref nums i) c)))))
(lambda (str)
(let* ((ustr (string-upcase str))
(f (string-ref ustr 0))
(skip (trans f)))
(let* ((mstr (string-map trans (string-delete ustr skipchars 1)))
(dstr (string-map (lambda (c)
(cond ((eq? c skip) #\0)
(else (set! skip c) c)))
mstr))
(zstr (string-delete dstr #\0)))
(substring (string-append (make-string 1 f) zstr "000") 0 4))))))
(soundex "Knuth") (soundex "Kant") (soundex "Lloyd") (soundex "Ladd")
#!/usr/local/bin/guile -s
!#
(use-modules (srfi srfi-13)
(srfi srfi-14)
(ice-9 rw)
(ice-9 regex))
(define data "analysed => analyzed
built-in => builtin
chastized => chastised
commandline => command-line
de-allocate => deallocate
dropin => drop-in
hardcode => hard-code
meta-data => metadata
multicharacter => multi-character
multiway => multi-way
non-empty => nonempty
non-profit => nonprofit
non-trappable => nontrappable
pre-define => predefine
preextend => pre-extend
re-compiling => recompiling
reenter => re-enter
turnkey => turn-key")
(define input (if (null? (cdr (command-line)))
(current-input-port)
(open-input-file (cadr (command-line)))))
(let* ((newline-char-set (string->char-set "\n"))
(assoc-char-set (string->char-set " =>"))
(dict (map
(lambda (line)
(string-tokenize line assoc-char-set))
(string-tokenize data newline-char-set)))
(dict-match (string-join (map car dict) "|")))
(let loop ((line (read-line input)))
(cond ((not (eof-object? line))
(regexp-substitute/global
(current-output-port) dict-match line
'pre
(lambda (x)
(cadr (assoc (match:substring x 0) dict)))
'post)
(loop (read-line input 'concat))))))
(close-port input)
(if (string-match "[^\\d]" str) (display "has nondigits"))
(or (string-match "^\\d+$" str) (display "not a natural number"))
(or (string-match "^-?\\d+$" str) (display "not an integer"))
(or (string-match "^[\\-+]?\\d+$" str) (display "not an integer"))
(or (string-match "^-?\\d+\.?\d*$" str) (display "not a decimal number"))
(or (string-match "^-?(\d+(\.\d*)?|\.\d+)$" str)
(display "not a decimal number"))
(or (string-match "^([+-]?)(\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$" str)
(display "not a C float"))
(define num1 (string->number str))
(define num2 (read))
(define (approx-equal? num1 num2 accuracy)
(< (abs (- num1 num2)) (expt 10.0 (- accuracy))))
(define wage 536) (define week (* 40 wage)) (format #t "One week's wage is: $~$\n" (/ week 100.0))
(round num) (inexact->exact num)
(use-modules (ice-9 format))
(define a 0.255)
(define b (/ (round (* 100.0 a)) 100.0))
(format #t "Unrounded: ~F\nRounded: ~F\n" a b)
(format #t "Unrounded: ~F\nRounded: ~,2F\n" a a)
(define a '(3.3 3.5 3.7 -3.3))
(display "number\tint\tfloor\tceil\n")
(for-each
(lambda (n)
(format #t "~,1F\t~,1F\t~,1F\t~,1F\n"
n (round n) (floor n) (ceiling n)))
a)
(define (dec->bin num)
(number->string (string->number num 10) 2))
(define (bin->dec num)
(number->string (string->number num 2) 10))
(define num (bin->dec "0110110")) (define binstr (dec->bin "54"))
(do ((i x (1+ i))) ((> i y)) )
(let ((i x))
(while (<= i y)
(set! i (1+ i))))
(let loop ((i x))
(cond ((<= i y)
(loop (+ i 7)))))
(display "Infancy is: ")
(do ((i 0 (1+ i)))
((> i 2))
(format #t "~A " i))
(newline)
(display "Toddling is: ")
(let ((i 3))
(while (<= i 4)
(format #t "~A " i)
(set! i (1+ i))))
(newline)
(display "Childhood is: ")
(let loop ((i 5))
(cond ((<= i 12)
(format #t "~A " i)
(loop (1+ i)))))
(newline)
(use-modules (ice-9 format))
(format #t "Roman for ~R is ~:@R\n" 15 15)
(random 5) (random 5.0)
(use-modules (srfi srfi-13) (srfi srfi-14))
(define chars (char-set->string char-set:graphic))
(define size (char-set-size char-set:graphic))
(define password
(string-unfold (lambda (x) (= x 8))
(lambda (x) (string-ref chars (random size)))
1+ 0))
(seed->random-state (current-time))
(define state (copy-random-state))
(random:uniform)
(random:uniform state)
(use-modules (ice-9 rw))
(define make-true-random
(letrec ((bufsize 8)
(accum (lambda (c acc) (+ (* 256 acc)
(char->integer c))))
(getbuf (lambda ()
(call-with-input-file "/dev/urandom"
(lambda (p)
(let ((buf (make-string bufsize)))
(read-string!/partial buf p)
buf))))))
(lambda (rand-proc)
(lambda args
(let ((state (seed->random-state (string-fold accum 0 (getbuf)))))
(apply rand-proc (append args (list state))))))))
(define urandom (make-true-random random))
(define urandom:exp (make-true-random random:exp))
(define urandom:normal (make-true-random random:normal))
(define urandom:uniform (make-true-random random:uniform))
(random:exp) (random:normal) (random:uniform)
(random:hollow-sphere! v)
(random:normal-vector! v)
(random:solid-sphere! v)
(define pi 3.14159265358979)
(define (degrees->radians deg)
(* pi (/ deg 180.0)))
(define (radians->degrees rad)
(* 180.0 (/ rad pi)))
(define (degree-sine deg)
(sin (degrees->radians deg)))
(sin z)
(cos z)
(tan z)
(asin z)
(acos z)
(atan z)
(acos 3.7)
(log z) (log10 z)
(define (log-base n z)
(/ (log z) (log n)))
(define (make-log-base n)
(let ((divisor (log n)))
(lambda (z) (/ (log z) divisor))))
(define log2 (make-log-base 2))
(log2 1024)
(define a (make-array 0 rows cols))
(array-set! a 3 row col)
(array-ref a row col)
(define b (make-array 0.0 x y z))
'#2((#f #f) (#f #t))
(define (matrix-mult m1 m2)
(let* ((d1 (array-dimensions m1))
(d2 (array-dimensions m2))
(m1rows (car d1))
(m1cols (cadr d1))
(m2rows (car d2))
(m2cols (cadr d2)))
(if (not (= m1cols m2rows))
(error 'index-error "matrices don't match"))
(let ((result (make-array 0 m1rows m2cols)))
(do ((i 0 (1+ i)))
((= i m1rows))
(do ((j 0 (1+ j)))
((= j m2cols))
(do ((k 0 (1+ k)))
((= k m1cols))
(array-set! result (+ (array-ref result i j)
(* (array-ref m1 i k)
(array-ref m2 k j)))
i j))))
result)))
(matrix-mult '#2((3 2 3) (5 9 8)) '#2((4 7) (9 3) (8 1)))
(define i 0+1i) (define i (sqrt -1))
(complex? i) (real-part i) (imag-part i)
(* 3+5i 2-2i) (sqrt 3+4i)
(inexact->exact (real-part (- (exp (* pi 0+1i)))))
#b01101101 #o155 #d109 #x6d
(define number (string->number hexadecimal 16))
(define number (string->number octal 8))
(format #t "~B ~O ~D ~X\n" num num num num)
(let loop ((perm (read-line)))
(cond ((not (eof-object? perm))
(format #t "The decimal value is ~D\n" (string->number perm 8))
(loop (read-line)))))
(use-modules (ice-9 format))
(format #t "~:D\n" (random 10000000000000000))
(format #t "~,,'.:D\n" (random 10000000000000000))
(define (commify num)
(let ((int (inexact->exact (truncate num))))
(if (= num int)
(format #f "~:D" int)
(string-append (format #f "~:D" int)
(let ((str (format #f "~F" num)))
(substring str (or (string-index str #\.)
(string-length str))))))))
(format #t "It took ~D hour~P\n" hours hours)
(format #t "It took ~D centur~@P\n" centuries centuries)
(define noun-plural
(let* ((suffixes '(("ss" . "sses")
("ph" . "phes")
("sh" . "shes")
("ch" . "ches")
("z" . "zes")
("ff" . "ffs")
("f" . "ves")
("ey" . "eys")
("y" . "ies")
("ix" . "ices")
("s" . "ses")
("x" . "xes")
("ius" . "ii")))
(suffix-match
(string-append "(" (string-join (map car suffixes) "|") ")$"))
(suffix-rx (make-regexp suffix-match)))
(lambda (noun)
(let ((m (regexp-exec suffix-rx noun)))
(if m
(string-append (regexp-substitute #f m 'pre)
(cdr (assoc (match:substring m) suffixes)))
(string-append noun "s"))))))
#!/usr/local/bin/guile -s
!#
(define (factor n)
(let ((factors '())
(limit (inexact->exact (round (sqrt n))))
(twos 0))
(while (even? n)
(set! n (ash n -1))
(set! twos (1+ twos)))
(if (> twos 0) (set! factors (list (cons 2 twos))))
(let loop ((i 3))
(let ((r (remainder n i)))
(cond ((= r 0)
(set! n (quotient n i))
(let* ((old-val (assv i factors))
(new-val (if old-val (1+ (cdr old-val)) 1)))
(set! factors (assv-set! factors i new-val)))
(loop i))
((< i limit)
(loop (+ 2 i))))))
(if (> n 1) (set! factors (cons (cons n 1) factors)))
(reverse! factors)))
(define (pp-term pair)
(if (= (cdr pair) 1)
(number->string (car pair))
(format #f "~A^~A" (car pair) (cdr pair))))
(for-each
(lambda (n)
(let ((factors (factor n)))
(format #t "~A = ~A" n (pp-term (car factors)))
(for-each
(lambda (x) (format #t " * ~A" (pp-term x)))
(cdr factors))
(newline)))
(map string->number (cdr (command-line))))
(current-time) (gettimeofday)
(localtime time) (gmtime time)
(tm:sec time) (set-tm:sec time secs) (tm:min time) (set-tm:min time mins) (tm:hour time) (set-tm:hour time hours) (tm:mday time) (set-tm:mday time mday) (tm:mon time) (set-tm:mon time month) (tm:year time) (set-tm:year time year) (tm:wday time) (set-tm:wday time wday) (tm:yday time) (set-tm:yday time yday) (tm:isdst time) (set-tm:isdst time isdst) (tm:gmtoff time) (set-tm:gmtoff time off) (tm:zone time) (set-tm:zone time zone)
(format #t "Today is day ~A of the current year.\n"
(tm:yday (localtime (current-time))))
(use-modules (srfi srfi-19))
(define now (current-date))
(date-nanosecond now) (date-second now) (date-minute now) (date-hour now) (date-day now) (date-month now) (date-year now) (date-year-day now) (date-week-day now) (date-week-number now start) (date-zone-offset now)
(format #t "Today is day ~A of the current year.\n"
(date-year-day (current-date)))
(use-modules (ice-9 format))
(let ((now (localtime (current-time))))
(format #t "The current date is ~4'0D ~2'0D ~2'0D\n"
(+ 1900 (tm:year now)) (tm:mon now) (tm:mday now)))
(use-modules (srfi srfi-19) (ice-9 format))
(let ((now (current-date)))
(format #t "The current date is ~4'0d-~2'0D-~2'0D\n"
(date-year now) (date-month now) (date-day now)))
(display (strftime "%Y-%m-%d\n" (localtime (current-time))))
(define time (localtime (current-time)))
(set-tm:mday time mday)
(set-tm:mon time mon)
(set-tm:year time year)
(car (mktime time))
(use-modules (srfi srfi-19))
(date->time-monotonic
(make-date nanosecond second minute hour day month year zone-offset))
(let ((time (localtime seconds))) (format #t "Dateline: ~2'0d:~2'0d:~2'0d-~4'0d/~2'0d/~2'0d\n"
(tm:hour time) (tm:min time) (tm:sec time)
(+ 1900 (tm:year time)) (1+ (tm:mon time)) (tm:mday time)))
(use-modules (srfi srfi-19))
(let* ((time (make-time time-monotonic nanosecond second)))
(display (date->string (time-monotonic->date time) "~T-~1\n")))
(define when (+ now difference))
(define then (- now difference))
(define birthtime 96176750)
(define interval