Chicken Scheme modules

worker.scm

(declare (unit worker)) ;; required for static build, remove in dynamic build
(module worker
    (make-worker)

(import scheme)

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

) ;; end module worker

main.scm

#!/usr/bin/env csi -s

(import scheme
    (chicken load)
    (chicken process-context)
)

;; repeat for each library
(cond-expand
    (compiling (declare (uses worker)))  ;; required for static build, remove for dynamic build
    (else (load-relative "worker.scm")) )
(import worker)

(define (main argv)
    (let ( (w (make-worker 0)) )
        (print "worker: " (w))
        (print "worker: " (w))
        (print "worker: " (w)) ) )

(main (command-line-arguments)) ;; csi -ss will call main, but no such convenience exists in csc

build.sh

#!/bin/sh

buildtype=$1

if [[ $buildtype == static ]]; then
    csc -c -j worker worker.scm -o worker.o || exit $?
    csc -c main.scm -o main.o || exit $?
    csc main.o worker.o -o main || exit $?  # add -static for a binary with no Chicken dependencies
elif [[ $buildtype == dynamic ]]; then
    csc -j worker worker.scm -o worker.so || exit $?
    csc main.scm -o main || exit $?
else
    echo "Usage: ./build.sh [dynamic|static]"
    exit 1
fi

rm -f *.o *.import.scm *.link

run as script

% ./main.scm

build & run as binary

% ./build.sh static
% ./main

One thought on “Chicken Scheme modules”

  1. My Scheme Stuff: Category: Scheme: My blog posts on Scheme. scheme-test-unit: My unit test runner for SRFI-64. Basic Games in Scheme: Classic BASIC games &…

Comments are closed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)

Mentions

  • My Scheme Stuff: Category: Scheme: My blog posts on Scheme. scheme-test-unit: My unit test runner for SRFI-64. Basic Games in Scheme: Classic BASIC games &…

Mentions

  • Mark writes