5 ms·
Some Schemes - most notably Racket, which I use the most - allow for nested defines. Your code actually is a valid Racket code after polishing the differences i
by klibertp 11y ago
Some Schemes - most notably Racket, which I use the most - allow for nested defines. Your code actually is a valid Racket code after polishing the differences in naming and syntax a bit:
#lang racket
(define-syntax-rule (defun name (arg ...) . body)
(define (name arg ...) . body))
(define nil null)
(define t #t)
(defun ab (l)
;; "I'm too lazy to reverse the result back."
(defun _ (l a b f)
(if (null? l)
(if f (list a b) (list b a))
(_ (cdr l) b (cons (car l) a) (not f))))
(_ l nil nil t))
(ab '())
(ab '(1))
(ab '(1 2))
(ab '(1 2 3 4 5 6 7 8 9 10))
;; ((9 7 5 3 1) (10 8 6 4 2))
(ab '(1 2 3 4 5 6 7 8 9 10 11))
;; ((11 9 7 5 3 1) (10 8 6 4 2))
...and you don't need to worry about recursion limits thanks to mandatory TCO.
- agumonkey 11y agoYeah, I spent too much time on scheme classes about recursive logic while using emacs lisp at the same time, hence the code above.