Racketifying goof-loop

I have racketified goof loops. No :for or :acc needed, and :when, :unless, and :break are no longer parethesised.
:acc blah (in ...) and :for blah (in ...) has been changed to
(blah (folding ...)) and (blah (in ...))
This commit is contained in:
Linus 2020-11-09 13:30:02 +01:00
parent 716c26c7ce
commit 2c182da570
3 changed files with 101 additions and 113 deletions

View file

@ -37,6 +37,15 @@
;; hashqing
;; hashving
(define-syntax in
(syntax-rules ()
((_ ((var) (init)) n . rest)
(n () () ((var init var)) () () () . rest))
((_ ((var) (init step)) n . rest)
(n () () ((var init step)) () () () . rest))
((_ ((var) (init step stop)) n . rest)
(n () () ((var init step)) (stop) () () . rest))))
(define-syntax in-list
(syntax-rules ()
((_ ((var) source) next . rest)
@ -150,24 +159,26 @@
()
. rest))))
;;> \macro{(for ch (in-file [input-port [reader [eof?]]]))}
;; ;;> \macro{(for ch (in-file [input-port [reader [eof?]]]))}
;; (define-syntax in-file
;; (syntax-rules ()
;; ((in-file ((var) source) next . rest)
;; (in-file ((var p) source) next . rest))
;; ((in-file ((var p) (file)) next . rest)
;; (in-file ((var p) (file read-char)) next . rest))
;; ((in-file ((var p) (file reader)) next . rest)
;; (in-file ((var p) (file reader eof-object?)) next . rest))
;; ((in-file ((var p) (file reader eof?)) next . rest)
;; (next ((p (open-input-file file)) (r reader) (e? eof?))
;; ()
;; ((var (r p) (r p)))
;; ((e? var))
;; ()
;; ((dummy (clo
;; se-input-port p)))
;; . rest))))
(define-syntax in-file
(syntax-rules ()
((in-file ((var) source) next . rest)
(in-file ((var p) source) next . rest))
((in-file ((var p) (file)) next . rest)
(in-file ((var p) (file read-char)) next . rest))
((in-file ((var p) (file reader)) next . rest)
(in-file ((var p) (file reader eof-object?)) next . rest))
((in-file ((var p) (file reader eof?)) next . rest)
(next ((p (open-input-file file)) (r reader) (e? eof?))
()
((var (r p) (r p)))
((e? var))
()
((dummy (close-input-port p)))
. rest))))
(define-syntax in-generator
(syntax-rules ()
@ -187,29 +198,15 @@
((up-from (() . args) next . rest)
(up-from ((var) . args) next . rest))
((up-from ((var) (start (to limit) (by step))) next . rest)
(next ((s start) (l limit) (e step))
()
((var s (+ var e)))
((>= var l))
()
()
. rest))
(next ((s start) (l limit) (e step)) ()
((var s (+ var e))) ((>= var l)) () () . rest))
((up-from ((var) (start (to limit))) next . rest)
(next ((s start) (l limit))
()
(next ((s start) (l limit))()
((var s (+ var 1)))
((>= var l))
()
()
. rest))
((>= var l)) () () . rest))
((up-from ((var) (start (by step))) next . rest)
(next ((s start) (e step))
()
((var s (+ var e)))
()
()
()
. rest))
(next ((s start) (e step))()
((var s (+ var e))) () () () . rest))
((up-from ((var) (start)) next . rest)
(next ((s start))
()
@ -218,7 +215,11 @@
()
()
. rest))
))
;; Extra convenience, make it act like (in-range ...) from racket, but only for positive numbers.
((up-from ((var) (start limit step)) next . rest)
(next ((s start) (l limit) (e step)) () ((var s (+ var e))) ((>= var l)) () () . rest))
((up-from ((var) (start limit)) next . rest)
(up-from ((var) (start limit 1)) next . rest))))
;;> \macro{(for x (down-from [start] [(to limit)] [(by step)]))}
@ -283,6 +284,20 @@
((var (final cursor)))
. rest))))
(define-syntax folding
(syntax-rules (if)
((_ ((var) (init update (if guard))) n . rest)
(n ()
((var init (if guard update var)))
() () ()
((var var))
. rest))
((_ ((var) (init update)) n . rest)
(folding ((var) (init update (if #t))) n . rest))
((_ ((var) (init)) n . rest)
(folding ((var) (init var (if #t))) n . rest))))
;;> \macro{(for x [pair] (listing expr))}
(define-syntax listing