(define (range n) ...
The "parenthesis pileup" aside, the following things jump out at me:
1) Your "range" function already exists, called "iota" (after the iota operation in APL).
2) Even if it didn't exist, your recursive definition is messy.
This is more idiomatic:
(define (range n)
(define (go n a)
(if (< n 0)
a
(go (1- n) (cons n a))))
(go n 0))
I would write it without the second define using a named let:
(define (range n)
(let go ((n n) (a '())) ...
(define (range n)
(let go ((n n) (a '()))
(if (< n 0)
a
(go (1- n) (cons n a)))))
On Fri, 01 Mar 2024 11:50:36 +0100, Andreas Eder wrote:
(define (range n)
(let go ((n n) (a '()))
(if (< n 0)
a
(go (1- n) (cons n a)))))
Interesting. r6rs (section 11.4.6) doesn’t seem to allow that form. Also
it would seem you would need “letrec” rather than “let”, but that doesn’t
work for me.
Named Let is not a binding construct - it's a looping construct.
See 11.16
George Neuner <gneuner2@comcast.net> writes:
Named Let is not a binding construct - it's a looping construct.
See 11.16
Thanks, I wonder if that is something relatively recent (i.e. arrived
between r4rs and r6rs). It is kind of ugly and I'm used to seeing
nested defines. Maybe there are some situations where the named let is
more convenient. Or maybe I can get used to it.
Part of the idea of Guile was to be the execution engine for various
other languages that would get transpiled to Scheme, but idk if that
went anywhere.
On Sun, 3 Mar 2024 22:56:25 +0000, HenHanna wrote:
it's written in a [functional] or [mathematical] or "comprehensive" style.
Yup, I like writing functional constructs in primarily-procedural
languages. It’s better than trying to work in supposedly pure-functional languages.
Python also uses the term “comprehension” for certain uses of that kind of construct.
and not Perlis?
I like another quote of his: “There are two ways to write error-free programs; only the third one works.”
I would write it without the second define using a named let:
(define (range n)
(let go ((n n) (a '()))
(if (< n 0)
a
(go (1- n) (cons n a)))))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 146:44:53 |
Calls: | 10,383 |
Calls today: | 8 |
Files: | 14,054 |
D/L today: |
2 files (1,861K bytes) |
Messages: | 6,417,714 |