Gambit Scheme modules

worker.sld

(define-library (worker)
    (import (scheme base))
    (export
        make-worker
    )
    (begin

(include "./worker.scm")

)) ;; define-library worker

worker.scm

(define (make-worker n)
    (lambda ()  (set! n (+ n 1))  n) )

main.scm

#!/usr/bin/env -S gsi-script -:search=.

(import (gambit)
    (worker)
)

;;(main) with variable args is magically called by gsi-script & compiler
(define (main . argv)
    (println "argv: " argv)
    (let ( (w (make-worker 0)) )
        (println "worker: " (w))
        (println "worker: " (w))
        (println "worker: " (w)) )
)

run as script

% ./main.scm 666
argv: 666
worker: 1
worker: 2
worker: 3

build.zsh

#!/bin/zsh
export GAMBOPT=search=.
gsc -o main -exe . worker.sld main.scm

(make sure to import each of the sld library definitions, NOT the scm library impl!)

compile

% ./build.zsh
worker.sld:
.../worker.c:
main.scm:
.../main.c:
.../main_.c:
% ./main 666
argv: 666
worker: 1
worker: 2
worker: 3

Comments are closed.