1. brev-separate
    1. Making macros
      1. define-syntax-rules
      2. define-ir-syntax*
      3. define-ir-syntax
    2. Making procedures
      1. define-closure
      2. match-define
      3. call-table, call-table*, call-vector, call-string, call-list, and call-record
      4. call-key*
      5. ct, ctq, ct*, ctq*
      6. define-some
      7. define-curry
      8. c a.k.a. {{πŸ›}} a.k.a. @>
      9. fn
      10. over
      11. as-list
      12. make-tree-accessors
      13. make-sloppy-tree-accessors
    3. Making values
      1. with-result
      2. aif-with-result
      3. empty?
      4. eif and econd
      5. like?
      6. is?
      7. scdr, and scar
      8. normalize-absolute-pathname
      9. slice
      10. descend
    4. Doing stuff
      1. for-each-line
      2. niy
    5. Source code

This is brev-separate, a miscellaneous hodge-podge of macros and procedures that all have the shared aim of brevity. Sort of my take on the clojurian and miscmacros and (chicken base) genre.

It's called brev-separate since the full brev egg also imports and reexports a bunch of other eggs, including the aforementioned clojurian and miscmacros.

Making macros

Chicken has syntax-rules macros and ir-macros. Let's shave off some of the boiler plate so that it's easier to make macros.


There is already the wonderful define-syntax-rule in miscmacros but here is also define-syntax-rules:

(define-syntax-rules foo ()
  ((foo bar) (+ bar 3))
  ((foo bar baz) (* bar baz)))

It's just the ultra lazy person's shorthand for:

(define-syntax foo
  (syntax-rules ()
    ((foo bar) (+ bar 3))
    ((foo bar baz) (* bar baz))))


define-ir-syntax* is more interesting.

(define-ir-syntax* name
  (pattern body) ...)

It uses matchable to dispatch between different call signatures (kinda similar to how syntax-rules work) while also allowing you to inject, compare, strip-syntax and syntax inside, as per usual with ir-macros.

Here's an example:

  (aif test yes no)
  `(let ((,(inject 'it) ,test))
     (if ,(inject 'it) ,yes ,no)))

(aif (... expensive test ...)
     (car it)
     (print "oh! no!"))

When you have multiple call-signatures, wrap pattern / body set with parens.

  ((aif #f yes no) no)
  ((aif test yes no)
   `(let ((,(inject 'it) ,test))
      (if ,(inject 'it) ,yes ,no))))


Sometimes pattern matching is overkill or you have something else in mind.

define-ir-syntax macros are just

(define-ir-syntax name body)

where the body has access to body, inject, compare, strip-syntax and syntax, as in the following example:

(define-ir-syntax comp-prod
  (apply * body))

(comp-prod 2 3 4)

β‡’ 24

As a rule of thumb, if you are deliberately injecting new names into the namespace that's when you are using ir-macros, and when you want to avoid doing that, use syntax-rules.

Making procedures


(define-closure bindings head body ...)

This works like your normal

(define head body ...)

except that bindings are lexically closed over body.

(define-closure (x 0) (counter) (inc! x))

(counter) (counter) (counter)

β‡’ 1 2 3

The pairs of bindings aren't individual paren-wrapped, just alternating between name and expression. The set of bindings as a whole has parens.

(define-closure (x 0 y 10) (jolly) (list (inc! x) (dec! y)))

(jolly) (jolly) (jolly)

β‡’ (1 9) (2 8) (3 7)



Calling match-define directly is deprecated in favor of using the macro from match-generics, which can expand to it or to normal define as needed. (But match-define is still used internally to avoid circular dependencies.)

call-table, call-table*, call-vector, call-string, call-list, and call-record

The previous construct is generally useful so let's just provide it as call-table.

(define arity (call-table))
(define color (call-table))

(arity cons 2)
(color cons 'blue)

(map (cut <> cons) (list arity color))

β‡’ (2 blue)

call-table takes two optional keyword argument, default:, to set the default response for unknown keys, and seed: which can be a hash-table or an alist, and defaults to empty.

There is also call-table* which by default cons its values to a list instead of replacing them. It takes four keyword arguments. proc: which defaults to cons, initial which defaults to '(), and unary which defaults to #f, and seed: as above.

Both versions of call-table lets you access the underlying hash-table by calling them with no arguments, and to set them by calling them with the keyword argument update:.

(color update: my-other-hash-table)

Full documentation for call-tables Full documentation for callable arrays


Sometimes you think call-table is convenient but you only need one key.

For call-key, just use make-parameter.

But call-key* is awesome since it accumulates its values.

It has the same proc, unary, and initial keyword arguments as call-table*. It doesn't have seed because the idea is that you just use inititial. The generated procedure has update (which takes a new list as argument) and get (which you only need for unary call-keys).

(define horses (call-key*))

(horses 'ruby)
(horses 'kind-girl)
(horses 'tornado)


β‡’ (tornado kind-girl ruby)

ct, ctq, ct*, ctq*

This is sugar for creating call-tables with some values already filled.

The q variants are implicitly quasiquoted while the non-q variants aren't.


(let ((banana-color 'yellow))
  (ctq banana ,banana-color apple red))

is equivalent to

(let ((banana-color 'yellow))
  (call-table seed: `((banana . ,banana-color) (apple . red))))


(let ((banana-color 'yellow))
  (ct 'banana banana-color 'apple 'red))

The * variants create call-table* instances instead.

These call-tables aren't closed, you can add more keys and values to them.


This is for making functions that implicitly returns '() on an empty? first argument. In other words, it defines a body for patterns with some non-empty value as first argument, hence the name define-some.

For example,

(define-some (descseq num)
   (cons num (descseq (sub1 num))))

is shorthand for

(define (descseq num)
  (if (empty? num)
      (cons num (descseq (sub1 num)))))


(descseq 5)

β‡’ (5 4 3 2 1)


It's nice that you can make specific curries with the SRFI-219 style define heads (which is implemented per default in Chicken).

That's nice if you know exactly how many stragglers and how many immediate args you have, but sometimes you need the currying itself to be arbitrary arity.

Let's say you already have something like:

(define (foo bar baz bax)
  (print baz)
  (+ bar baz bax))

but you realize you need arbitrary-arity currying.

Just change it to use define-curry instead of define:

(define-curry (foo bar baz bax)
  (print baz)
  (+ bar baz bax))

 (foo 100 20 3)
 ((foo 100) 20 3)
 ((foo 100 20) 3)
 ((foo) 100 20 3)
 (((foo) 100) 20 3)
 (((foo 100) 20) 3)
 ((((foo) 100) 20) 3))

Prints seven 20 and returns #t.

It only works when foo otherwise would have fixed arity.

c a.k.a. {{πŸ›}} a.k.a. @>

This isn't the traditional c-combinator from mockingbirds and such. It's just a one-letter spelling of "curry". It's a function combinator.

((c + 1 20 300) 4000 50000)

β‡’ 54321

I also exported it using the name πŸ› for those who find emoji names more comfortable to use due to namespace issues.

I later found out that @> from the holes egg is same the combinator as this. Then, a few months later than that, I found out that partial from Clojure is the same combinator as this.

It has arbitrary arity and can work on arbitrary arity functions, but isn't recursive to multiple levels.


(fn body ...)

is shorthand for

(lambda some-basic-bindings body ...)

where some-basic-bindings is one of

and the fn macro automatically figures out which of those four you mean.


(over body ...)

is shorthand for

(cut map (lambda some-basic-bindings body ...) <>)

except that the map can take any number of lists and that i is also anaphorically bound to the list index in body.

Here is an example:

((over (+ x x y i))
 '(10 20 40) '(3 6 9))

β‡’ (23 47 91)


Here is a functional combinator for Scheme that lets its arguments treat their arguments as if they were lists.

((as-list (c filter odd?)) 130752)

β‡’ 1375

((as-list cdr reverse) 23311358)

β‡’ 5311332

((as-list delete-duplicates) 23311358)

β‡’ 23158

(define (vowel? l) ((as-list (c member l)) "aeiou"))
((as-list (c filter vowel?)) "magnetic mountaintop")

β‡’ β€œaeiouaio”

Together with over:

((as-list (over (if (vowel? x) x (char-upcase x)))) "fleet foxes")

β‡’ β€œFLeeT FoXeS”


Sometimes you just need an arbitrarily long tree dereferencer.

(c_r cddadadaddddar)

makes cddadadaddddar real. Works for any sequence of a's and d's.


As above, but uses scar and scdr instead of car and cdr.

Making values


This is something that is sometimes cozy:

 (print 1 2 (save 3) 4 5 6)
 (print 7 8))

Prints 123456 and 78, returns 3


(aif-with-result (odd? (save 3)) (+ it 4) (+ it 1000))

β‡’ 7

That tests the entire expression but only stores the saved part into it.

Combining aif and with-result would do the opposite:

 (with-result (pred? (save 27)))
 it #f)

That tests 27 and stores 27 into it, while the pred? call is thrown away.


This is a generic predicate to see if a string is "", a list is '(), a number is 0 etc.

eif and econd

eif is a version of if (or, to be precise, of aif since it anaphoric) that treats empty things as falsy.

(eif "" it 'no)

β‡’ no

econd, similarly, is an "empty is falsy" version of acond.


like? is a unary curried version of equal?


is? is a unary curried version of eq?

scdr, and scar

A scar is like a car but returns '() if the pair has no car. A scdr is like a cdr but returns '() if the pair has no cdr.


(normalize-absolute-pathname filename)

Uses current-directory to try to figure out an absolute path for filename.


Here is an generic slice multimethod for Scheme.

(slice '(hello now there you are) 1 3)

β‡’ (now there)

(slice "so this is where you are hiding" 3 7)

β‡’ (#\t #\h #\i #\s)

(let ((str "so this is where you are hiding"))
  (set! (slice str 3 7) "that")

β‡’ "so that is where you are hiding"

(slice 1243153 -3 -0)

β‡’ (1 5 3)

Because of Scheme's call-by-value semantics, set! doesn't work on numbers.β™₯


Descend is sort of like a named let except it does three magic things.

First of all, the let tag is always desc. I almost named the macro itself desc but that would've been bad since then you couldn't nest them.

Second of all, if the binding is to a value of the same name e.g. (lis lis) you can just put the name there. You can mix these shorthand bindings with normal bindings.

Now, if the first binding starts of as empty?, the third magic thing (which I'll get into shortly) is disabled and you can go on your merry way only using the above two magics.

(descend ((sum 0) (nums '(1 2 3 4)))
         (if (null? nums) sum
             (desc (+ sum (car nums)) (cdr nums))))

β‡’ 10

Otherwise, if it does start out non-empty...

(descend ((nums '(1 2 3 4)))
         (+ (car nums) (desc (cdr nums))))

β‡’ 10

That's right. It only recurs the value is non-empty.

Doing stuff


(for-each-line filename body ...)

body is called for its sideeffects once per line of text in filename with the variable line anaphorically bound to that line.

For example:

(for-each-line "/tmp/foo" (print line))



NIY stands for "not implemented yet".

Errors out if called with no arguments or if any of its arguments are true. Sort of like a living FIXME.

Source code

git clone