schemedoc / cookbook

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

void value #53

Closed jcubic closed 2 years ago

jcubic commented 2 years ago

I've seen this pattern in a book by Nils M Hom Sketchy Scheme. it can be written as a variable or as a macro:

(define void (if #f #f))

(define-syntax void
  (syntax-rules ()
    ((_) (if #f #f))))

This is a way to get undefined (or to be precise unspecified by R7RS spec) value in Scheme. But I'm not sure how much it's compatible with different scheme systems.

we can also add predicate:

(define (void? x)
  (equal? (if #f #f) x))

(define (void? x)
  (equal? x void))

Tested in Chicken, Guile, and Kawa (that gives warning warning - missing else where value is required).

lassik commented 2 years ago

That's a good try for a portable void value, but it's not bulletproof since as you say, it's unspecified by the standard.

Here's a survey of what (if #f #f) returns: https://doc.scheme.org/surveys/OneArmedIf/

lassik commented 2 years ago

In general, https://doc.scheme.org/surveys/ (source https://github.com/schemedoc/surveys) is a good place to find out how different implementations do things. If you discover new stuff, PRs to that repo are very helpful to the Scheme community!

lassik commented 2 years ago

Here's a new one: https://github.com/schemedoc/surveys/pull/9

lassik commented 2 years ago

@jcubic According to https://doc.scheme.org/surveys/OneArmedIf/ (if #f #f) is not a very reliable way of getting void. Should we omit the recipe?

jcubic commented 2 years ago

I'm fine.

lassik commented 2 years ago

Thanks!