2025-08-29

sph-lib examples

to use the features you have to import the relevant modules first, which are mentioned in the titles of the following sections. for example

(use-modules (sph list))

(sph)

display trace and debug messages

(+ 3 (debug-log (+ 1 2)))
(debug-log 1 2 3) -> 1

debug-log returns the first argument and writes all arguments formatted to standard output. intercept the execution flow for tracing without disturbing it

nullary

alternative to (lambda () body ...)

(call-with-values (nullary (partition a? b)) list)

let

let with simplified form that additionally makes binding just one variable a bit shorter

(let (a 1)
  body ...)

display-line

(display-line "test")

define-syntax-rule

(define-syntax-rule (test a b) (+ a b))

(define-syntax-rules test
  ((test a b) (+ a b))
  ((test a) a))

short-forms

l q qq
lambda quote quasiquote

l for lambda

(map (l (a) (+ a 1)) a)

as alternative to

(map (lambda (a) (+ a 1)) a)

quoting without auxillary read syntax

(q a)
(qq a)

as alternatives to

(quote a)
(quasiquote a)

(sph list)

map the integers in a given range

(map-integers 10 (l (n) n))

result

(0 1 2 3 4 5 6 7 8 9)

associative data structures with (sph alist) and (sph hashtable)

creation

key and value specified alternatingly

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

in application similar to

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

-q variants that implicitly quote keys

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

get an alist from a hashtable

; hashtable max-depth -> list:alist
(ht-alist ht)
(ht-alist ht 2)
(ht-alist ht (inf))

access

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

-q variants that implicitly quote keys

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

nested references

(alists-ref x 1 2 3)
(ht-tree-ref x 1 2 3)

(sph tree)

create filesystem paths from nested lists

(define input
  (q
    ("/usr"
      ("share" "guile")
      "include"
      "")))

(prefix-tree->path-list input)

result:

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

other features

  • port-lines-map: map over lines read from ports like standard input
  • tree-map: map over lists and sublists
  • nested scheme syntax config files with (sph lang config)
  • path-pipe-chain: link procedures or shell commands with temporary paths or pipes for data flow between them
  • call-at-interval
  • vector-accessor and vector-setter: procedures like (lambda () (vector-ref a 1))
  • vector-object: (myvector 3) - access vector elements like object properties