sph-lib examples

always implied

(import (sph common))

the (sph common) module currently includes (except (srfi srfi-1) map) (rnrs sorting) (rnrs exceptions) (sph alist) (sph filesystem) (sph hashtable) (sph io) (sph list) (sph number) (sph one) (sph process) (sph string) (sph vector) (sph)

l for lambda

(map (l (a) (+ a 1)) a)
; instead of
(map (lambda (a) (+ a 1)) a)

quoting without auxillary read syntax

(q a)
; instead of
(quote a)
(qq a)
; instead of
(quasiquote a)
(list-q 1 2 3)
; instead of
(quote (1 2 3))

trace and debug messages

the debug-log syntax form writes all arguments formatted to standard output and returns its first argument, so that it can be used to intercept the execution flow

(+ 3 (debug-log (+ 1 2)))
(debug-log (+ 1 2) a b)

associative data structures


key and value are specified alternatingly

(alist "a" 1 (q b) 2)
(alist-q a 1 b 2)
(ht-create "a" 1 (q b) 2)
(ht-create-symbol a 1 b 2)

similar to

(list 1 2 3)
(vector 4 5 6)


(alist-ref myalist (q a))
(ht-ref ht (q a))
(alist-ref-q myalist a)
(ht-ref-q ht a)

nested hashtables reference

(ht-tree-ref ht (q a) (q b) (q c))


this form can avoid additional nesting and indentation

the second argument is a flat list of prefixes that will be composed


(define-as binding-name identifier/(identifier ...) body ...)

expands to

(define binding-name (identifier body ...))

or, if multiple identifiers have been specified

(define binding-name (identifier (identifier body ...)))

the number of identifiers is not limited


(define-as b list

equivalent to

(define b

works with syntax transformers as well as procedures

(define-as ht ht-create-symbol a 1 b 2 c 3)


(define-as b (quote div div) (span 1))

equivalent to

(define b (quote (div (div (span 1)))))

create filesystem paths from lists

(import (sph tree))

(define input
      ("share" "guile")

(prefix-tree->path-list input)

creates the list

("/usr/share/guile" "/usr/include" "/usr/")

multiple result values and lambda pattern matching

  (l (a b c) #t)
  (list 1 2 3))

-> a:1 b:2 c:3

  (l (a b . c) #t)
  (list 1 2 3 4 5))

-> a:1 b:2 c:(3 4 5)

  (l (a . b) #t)
  (pair 1 2))

does unfortunately not work

  (l* (a #:optional b) #t)
  (list 1 2))

-> a:1 b:2. if a shorter list (list 1) where given instead, then a:1 b:#f

  (l* (a #:key (b 8) c) #t)
  (list 1 #:b 2 #:c 3))

special symbols starting with #: are used to differentiate them from normal symbols

tags: sph-lib start document guide pattern example computer