Could you share a short, VERY Readable Pythonic (or Lisp, Scheme) code
that solves this?
This is my answer after spending this afternoon learning Guile. Much
more wordy than the Python version I previously posted. Anybody know
how to do it better?
Note that the parenthesis style is pretty much universal in Lisp and variants.
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
This is my answer after spending this afternoon learning Guile. Much
more wordy than the Python version I previously posted. Anybody know
how to do it better?
The following works for me as a fairly simple port of the concise Python version to Guile.
On Tue, 27 Feb 2024 11:22:08 -0800, Paul Rubin wrote:
Note that the parenthesis style is pretty much universal in Lisp and
variants.
Sorry, not a fan of rCLparenthesis pileuprCY layout.
This is my answer after spending this afternoon learning Guile. Much
more wordy than the Python version I previously posted. Anybody know
how to do it better?
(define (range n) ...
1) Your "range" function already exists, called "iota" (after the iota operation in APL).
2) Similarly the "score" function looks translated from C to Scheme or something like that.
sum(a == b for a, b in zip(candidate, answer))
sum
(
i != j and a == b
for i, a in enumerate(candidate)
for j, b in enumerate(answer)
)
)
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
sum
(
i != j and a == b for i, a in enumerate(candidate)
for j, b in enumerate(answer)
)
In Python you might avoid the nested loops by writing that in terms of
set or multiset (collections.Counter) intersections.
On Tue, 27 Feb 2024 17:46:48 -0800, Paul Rubin wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
sum
(
i != j and a == b for i, a in enumerate(candidate)
for j, b in enumerate(answer)
)
In Python you might avoid the nested loops by writing that in terms of
set or multiset (collections.Counter) intersections.
Why would that be better?
In Python you might avoid the nested loops by writing that in terms ofWhy would that be better?
set or multiset (collections.Counter) intersections.
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Why would that be better?
You are trying to handle N digits and your algorithm does O(N**2) comparisons. Ok, I guess the whole search strategy is impractical if N
is larger than just a few, and in traditional Mastermind N=4, so maybe
that isn't an issue.
rCLPremature optimization is the root of all evil.rCY
Same idea as writing a matrix product as A*B instead of as some messy
thing with subscripts.
On Wed, 28 Feb 2024 15:54:48 -0800, Paul Rubin wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Why would that be better?
You are trying to handle N digits and your algorithm does O(N**2)
comparisons. Ok, I guess the whole search strategy is impractical if N
is larger than just a few, and in traditional Mastermind N=4, so maybe
that isn't an issue.
rCLPremature optimization is the root of all evil.rCY
-- variously attributed to Tony Hoare or Donald Knuth
Somebody still has to write the underlying code with the subscripts.
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Somebody still has to write the underlying code with the subscripts.
Sure, that's pushed down into a library or helper function though. Doing
it at the higher level is related to the "primitive obsession"
antipattern.
I only push things into separate/library functions if IrCOm going to reuse them. Splitting things off just for the sake of doing so is what we could call a rCLfragmentation smellrCY or a rCLgratuitous hierarchy antipatternrCY.
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) doesnrCOt seem to allow that form. Also >it would seem you would need rCLletrecrCY rather than rCLletrCY, but that doesnrCOt
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 2024-02-29, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
On Wed, 28 Feb 2024 15:54:48 -0800, Paul Rubin wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Why would that be better?
You are trying to handle N digits and your algorithm does O(N**2)
comparisons. Ok, I guess the whole search strategy is impractical if N
is larger than just a few, and in traditional Mastermind N=4, so maybe
that isn't an issue.
rCLPremature optimization is the root of all evil.rCY
-- variously attributed to Tony Hoare or Donald Knuth
Pinning it down more precisely at this stage would be premature
attribution.
yes, there are places where it doesn't bring much benefit to optimize,
but there are also the parts where it *does*.
On 2024-03-01, Kaz Kylheku wrote:
On 2024-02-29, Lawrence D'Oliveiro <ldo@nz.invalid> wrote: .....
rCLPremature optimization is the root of all evil.rCY
----- variously attributed to Tony Hoare or Donald Knuth
--- Synchronet 3.22a-Linux NewsLink 1.2Pinning it down more precisely at this stage would be premature attribution.
I sometimes feel that this specific sentence from Knuth's quote might be being used as a way to discourage even thinking about optimization, when
the intent of the whole quote might be actually the opposite: yes, there
are places where it doesn't bring much benefit to optimize, but there
are also the parts where it *does*.
it's written in a [functional] or [mathematical] or
"comprehensive" style.
and not Perlis?
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. ItrCOs better than trying to work in supposedly pure-functional languages.
Python also uses the term rCLcomprehensionrCY for certain uses of that kind of construct.
and not Perlis?
I like another quote of his: rCLThere are two ways to write error-free programs; only the third one works.rCY
41. Some programming languages manage to absorb change, but withstand progress. -------- For example?????
42. You can measure a programmer's perspective by noting his attitude on
the continuing vitality of FORTRAN.
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: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 50:09:18 |
| Calls: | 1,100 |
| Files: | 1,339 |
| Messages: | 275,859 |