Guile programming language
Created 2022-07-07 Updated 2024-01-14
Anaphoric if
(define-syntax aif-version-1
(lambda (x)
(syntax-case x ()
((_ test then else)
(with-syntax ((it (datum->syntax x 'it)))
#'(let ((it test))
(if it then else)))))))
(define-macro (aif-version-2 test then else)
`(let ((it ,test))
(if it ,then ,else)))
(aif-version-2 2 (+ 1 it) 'bomb) ; => 3
Responses to the above code:
help
,in (cerbo stats) ,binding ; to see what's available (help mid) ; get the document for a function MID
modules
Single: (use-modules (srfi srfi-19))
or multiple: (use-modules (srfi srfi-18) (srfi srfi-19))
srfi-19 Time/Date Library
ports
Read the output from a command:
(use-modules (ice-9 popen))
(define (read-command-v1 cmd)
(define port (open-input-pipe cmd))
(define str (reverse-list->string
(let loop ((char (read-char port))
(result '()))
(if (eof-object? char)
result
(loop (read-char port) (cons char result))))))
(close-pipe port)
str)
(define (read-command-v2 cmd)
(with-input-from-port (open-input-pipe cmd) read-string))
(display (read-command-v2 "ls -hal"))
See also:
procedures
cut: srfi-26
srfi
srfi-8 : receive: binding to multiple values
(receive (a b) (values 1 2) (displayln a) (displayln b)) ; 1 2
srfi-42 : Eager comprehensions
(list-ec (: i 5) (* i i)) => (0 1 4 9 16) do-ec append-ec string-ec string-append-ec vector-ec ...