schemedoc / cookbook

New Scheme Cookbook
https://cookbook.scheme.org
29 stars 3 forks source link

Nils M Holm code #34

Open jcubic opened 2 years ago

jcubic commented 2 years ago

Just got a message from Nils M Holm. He did few books about Scheme and pointed to:

http://t3x.org/s9fes/lib.html and http://t3x.org/s9fes/contrib.html

the code is from the book "Scheme 9 from Empty Space" all examples are in the public domain.

lassik commented 2 years ago

Great. Does he have a specific wish for what we should do with the code? Some of those files are quite big and require non-standard libraries, (load-from-library "foo.scm"). It would be a non-trivial amount of work to pick the procedures that would be a good fit for a cookbook format, and refactor them to use SRFIs where possible.

lassik commented 2 years ago

We should also go over the Schematics cookbook for the same purpose. The only question is who does all the work :)

Maybe we could split the work so each of us could try to send PRs for a few procedures per week. It would be a great help if Nils could prepare some of the code, or at least pick which files would be good for the cookbook.

jcubic commented 2 years ago

I'm looking at the code, there are some small examples, we can use those:

http://t3x.org/s9fes/adjoin.scm.html http://t3x.org/s9fes/collect.scm.html

I would leave the big ones maybe except for the amb operator:

http://t3x.org/s9fes/amb.scm.html

jcubic commented 2 years ago

But there is problematic define-syntax the code uses Lisp-like macros. Because the code is for Scheme implementation from scratch.

Will check only small functions and create a PR with those files.

lassik commented 2 years ago

amb is a Lisp classic, it would be great to have it in the cookbook.

jcubic commented 2 years ago

Maybe we can find it elsewhere or reimplement it with syntax-rules. I can try to write a scheme macro based on Nils's code.

lassik commented 2 years ago

The old book Teach Yourself Scheme in Fixnum Days explains and implements amb, but it's also using define-macro.

lassik commented 2 years ago

If you can rewrite it using syntax-rules, that would be great!

jcubic commented 2 years ago

It seems that Chicken Scheme doesn't support any way of nesting ellipsis in syntax-rules.

I've tested this: Both works in my Scheme implementation, the code is from my unit tests:

(define-syntax funcall
  (syntax-rules ::: ()
    ((_ name args :::) (name args :::))))

(display (funcall list 1 2 3))
(newline)

(define-syntax be-like-begin
  (syntax-rules ()
    ((be-like-begin name)
     (define-syntax name
       (syntax-rules ()
         ((name expr (... ...))
          (begin expr (... ...))))))))

(be-like-begin sequence)
(display (sequence 1 2 3 4))
(newline)
jcubic commented 2 years ago

But it seems both works in Guile.

lassik commented 2 years ago

These work in Chibi-Scheme, Chicken, Gauche, Kawa, Sagittarius:

(define-syntax funcall
  (syntax-rules ellipsis ()
    ((_ name args ellipsis) (name args ellipsis))))

(display (funcall list 1 2 3))
(newline)
(define-syntax be-like-begin
  (syntax-rules ()
    ((be-like-begin name)
     (define-syntax name
       (syntax-rules ellipsis ()
         ((name expr ellipsis)
          (begin expr ellipsis)))))))

(be-like-begin sequence)
(display (sequence 1 2 3 4))
(newline)
lassik commented 2 years ago

(syntax-rules ::: () doesn't work in Chicken because Chicken reads anything starting with : as a keyword, not a symbol. (About 10 Scheme implementations have keywords similar to Clojure, Common Lisp, and Emacs Lisp, though keywords are not part of the Scheme standards.)

sjamaan commented 2 years ago

Alternatively you could use |:::| to force the reader to see it as a symbol identifier, but that looks a bit ugly. It should be portable to all R7RS-compliant Schemes though.

lassik commented 2 years ago

From Nils:

Regarding the recipes for the cookbook: maybe this page is helpful:

http://t3x.org/s9fes/CATEGORIES.html