Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #74: You're out of memory


comp / comp.lang.scheme / Re: Parenthesis Pileup (was Re: (Mastermind) puzzle (with 3 digits) -- ...)

SubjectAuthor
* (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtHenHanna
`* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
 +* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
 |+* Re: Parenthesis Pileup (was Re: (Mastermind) puzzle (with 3 digits) -- ...)Lawrence D'Oliveiro
 ||`- Re: Parenthesis Pileup (was Re: (Mastermind) puzzle (with 3 digits) -- ...)Kaz Kylheku
 |`- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtKaz Kylheku
 `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
  +- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
  +* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  |`* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
  | `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  |  +- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtGeorge Neuner
  |  `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
  |   `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  |    +* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
  |    |`* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  |    | `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
  |    |  `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  |    |   `- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
  |    `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtKaz Kylheku
  |     `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtNuno Silva
  |      +- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  |      `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtHenHanna
  |       `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  |        `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtHenHanna
  |         `- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
  `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtAndreas Eder
   +- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
   +* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtLawrence D'Oliveiro
   |`* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtGeorge Neuner
   | `* Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtPaul Rubin
   |  `- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtGeorge Neuner
   `- Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code SoughtB. Pym

Pages:12
Subject: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: HenHanna
Newsgroups: comp.lang.scheme
Organization: A noiseless patient Spider
Date: Mon, 26 Feb 2024 04:27 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: HenHanna@gmail.com (HenHanna)
Newsgroups: comp.lang.scheme
Subject: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Sun, 25 Feb 2024 20:27:42 -0800
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <urh3vu$2br0h$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 26 Feb 2024 04:27:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2a69445d6b08e3756b6d877d3cc6915a";
logging-data="2485265"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/G2WALn0b0l5BgzC6+NTICMbnj2+vzsVA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Sv7gwuohwtzIgkcnBQ9J9wy2Nmk=
Content-Language: en-US
View all headers

(i just wrote (non-elegant) Python code.)

Could you share a short, VERY Readable Pythonic (or Lisp, Scheme) code
that solves this?

Thank you!

https://i.imgur.com/72LGJjj.jpeg

3 digit lock
[682]: One number is correct and well-placed
[614]: One number is correct but wrongly placed
[206]: Two numbers are correct but wrongly placed
[738]: Nothing is correct
[780]: One number is correct but wrongly placed

HINT -- A mark of a great puzzle, this one contains a surprise or two.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 04:59 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Tue, 27 Feb 2024 04:59:09 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 106
Message-ID: <urjq6t$31cu6$1@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 27 Feb 2024 04:59:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d339ad4b21c64d41c58c7c072f1b4d54";
logging-data="3191750"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19iq6B2UlhptpMGH7MpG22Y"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:MUuxM8snqkG2mygf717Ncz9H6gE=
View all headers

On Sun, 25 Feb 2024 20:27:42 -0800, HenHanna wrote:

> 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?

(import
(rnrs base)
(rnrs lists)
)

(define (range n)
; returns a list of integers from 0 up to n - 1 inclusive.
(letrec
(
(subrange
(lambda (n)
(cond
((>= n 0) (cons n (subrange (- n 1))))
(#t '())
) ; cond
) ; lambda
)
)
(reverse (subrange (- n 1)))
) ; let
) ; define

(define (score candidate answer)
(let
(
(in-right-place 0)
(in-wrong-place 0)
)
(for-each
(lambda (a)
(for-each
(lambda (b)
(when (eq? a b)
(set! in-wrong-place (+ in-wrong-place 1))
; might be in right place, fixed up below
) ; when
) ; lambda
answer
) ; for-each
) ; lambda
candidate
) ; for-each
(for-each
(lambda (a b)
(when (eq? a b)
(set! in-right-place (+ in-right-place 1))
(set! in-wrong-place (- in-wrong-place 1))
) ; when
) ; lambda
candidate
answer
) ; for-each
(list in-right-place in-wrong-place)
) ; let
) ; score

(define required-scores
'(
((6 8 2) (1 0))
((6 1 4) (0 1))
((2 0 6) (0 2))
((7 3 8) (0 0))
((7 8 0) (0 1))
)
)

(for-each
(lambda (n)
(let-values
(
((a b c answer) (values #f #f #f #f))
)
(set! a (div n 100))
(set! n (- n (* a 100)))
(set! b (div n 10))
(set! c (- n (* b 10)))
(set! answer (list a b c))
(when
(for-all
(lambda (candidate)
(let
(
(required-score (cadr candidate))
)
(set! candidate (car candidate))
(equal? (score candidate answer) required-score)
) ; let
) ; lambda
required-scores
) ; for-all
(display answer)
(display "\n")
) ; when
) ; let-values
) ; lambda
(range 1000)
) ; let

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 19:22 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Tue, 27 Feb 2024 11:22:08 -0800
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <87le75emqn.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91574d2575cac45da5d1f33a64b22cc2";
logging-data="3546858"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18irt6GUxgAynrEv0hmgpSm"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:208XYVM7RMl0i+Nkxjgqa9yMPhg=
sha1:fmayJ2FLnwvJMPXS5rADC078+rM=
View all headers

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. Note that the parenthesis style is pretty much
universal in Lisp and variants. Editors support it, and automatic paren
balancing in the editors keep them from getting too confusing.

If you want to understand Scheme, I suggest reading through SICP (click
the open access link at mitpress.mit.edu/sicp for a download). That
said, I'm not much of a Scheme user myself, so the below might have some
style issues.

I think by now, the methods introduced in Scheme have moved on to newer
languages like OCaml and then Haskell, so you might want to study those
instead. learnyouahaskell.com is a good start with Haskell.

================================================================

(use-modules (srfi srfi-1)
(srfi srfi-11))

(define clues '((682 1 0) (614 0 1) (206 0 2) (738 0 0) (780 0 1)))
(define (digits n)
(values (quotient n 100) (remainder (quotient n 10) 10) (remainder n 10)))
(define (count . args) (length (filter identity args)))

(define (score candidate answer)
(let-values (((a b c) (digits candidate))
((x y z) (digits answer)))
(let ((well-placed (count (= a x) (= b y) (= c z)))
(wrongly-placed (count (or (= a y) (= a z))
(or (= b x) (= b z))
(or (= c x) (= c y)))))
(values well-placed wrongly-placed))))

(define (test)
(let ((n 682) (a 1) (b 0))
(let-values (((well-placed wrongly-placed) (score n 042)))
(and (= a well-placed) (= b wrongly-placed)))))

(define (check candidate)
(define (check1 clue)
(let ((n (car clue))
(a (cadr clue))
(b (caddr clue)))
(let-values (((well-placed wrongly-placed) (score candidate n)))
(and (= a well-placed) (= b wrongly-placed)))))
(every check1 clues))

(display (filter check (iota 1000)))
(newline)

Subject: Re: Parenthesis Pileup (was Re: (Mastermind) puzzle (with 3 digits) -- ...)
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 20:21 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: Parenthesis Pileup (was Re: (Mastermind) puzzle (with 3 digits)
-- ...)
Date: Tue, 27 Feb 2024 20:21:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <urlg82$3d1ah$2@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87le75emqn.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 27 Feb 2024 20:21:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d339ad4b21c64d41c58c7c072f1b4d54";
logging-data="3573073"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+N17X02JL14FkNmhFxdJT5"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:9T0P09H/qQwwrZeAgGJs8nTcjk8=
View all headers

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 “parenthesis pileup” layout.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Kaz Kylheku
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 20:51 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Tue, 27 Feb 2024 20:51:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <20240227124603.164@kylheku.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87le75emqn.fsf@nightsong.com>
Injection-Date: Tue, 27 Feb 2024 20:51:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="923ed161601c41226170687a2c58a566";
logging-data="3585224"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DHmh0xXK5ZjY9llvWaQIabCGD1DsOgPY="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:zXVZSeEY8X4Tn2SSSyjYKG5uNmQ=
View all headers

On 2024-02-27, Paul Rubin <no.email@nospam.invalid> wrote:
> 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.

Based on code formatting alone, I'm declaring yours vastly better.

The problem is actually trivial.

All you have to do is close your parentheses ))) like a sane person, and
the solution will pop out sooner or later.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Subject: Re: Parenthesis Pileup (was Re: (Mastermind) puzzle (with 3 digits) -- ...)
From: Kaz Kylheku
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 20:56 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: Parenthesis Pileup (was Re: (Mastermind) puzzle (with 3 digits)
-- ...)
Date: Tue, 27 Feb 2024 20:56:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <20240227125204.655@kylheku.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87le75emqn.fsf@nightsong.com> <urlg82$3d1ah$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 27 Feb 2024 20:56:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="923ed161601c41226170687a2c58a566";
logging-data="3585224"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DDioKvHTaXXhmNkD64Se2SlhsUr4gh6Q="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:FTWwHBh64ifPFqm0essbVpzuE88=
View all headers

On 2024-02-27, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
> 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 “parenthesis pileup” layout.

You've certainly come to the right language family, if you want to
forever hack by yourself, doing things Your Way.

I suspect you're doing your formatting manually, though, which takes
more effort. The parentheses pileup is well supported in editors.

If you don't want collaborators, you can just communicate that
explicitly (e.g. a block comment saying ";; this is my solo work,
patches will be rejected regardless of quality") and not inflict an
unergonomic way of working on yourself.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 21:36 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Tue, 27 Feb 2024 13:36:25 -0800
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <87edcxegiu.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91574d2575cac45da5d1f33a64b22cc2";
logging-data="3606481"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+xC/MN97VYHUZF/ugo/xGw"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:HeS223VcWQnNF9fjHVniqwm33QY=
sha1:RU9/MALkGryngpB1xodFSYRCGiM=
View all headers

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 "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))

Note that the accumulation parameter in "go" makes go tail recursive, so
it uses a fixed number of stack cells.

2) Similarly the "score" function looks translated from C to Scheme or
something like that. Generally, the use of set! is a code smell in
Scheme. It's preferable to use recursion and combinators like map and
filter. In Haskell, set! doesn't even exist in any convenient form.

Without destructive updates, you end up using a programming style with a
rather different set of idioms. Scheme was an early enabler of that
style. Lisp predated Scheme and was sort of an intermediate step.
See the SICP book for a deeper intro.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 21:46 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Tue, 27 Feb 2024 13:46:57 -0800
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <87a5nleg1a.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91574d2575cac45da5d1f33a64b22cc2";
logging-data="3606481"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QKDOu5OwRfbX0bm/hrZdD"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:aNJJGq0o01DUm1V0NK6r7NQkRsY=
sha1:1L20owNDdVP7kNB0h2oUCAT+KCY=
View all headers

Paul Rubin <no.email@nospam.invalid> writes:
> (define (range n) ...

Oops:

(define (range n)
(define (go n a)
(if (< n 0)
a
(go (1- n) (cons n a))))
(go (1- n) '()))

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 27 Feb 2024 22:46 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Tue, 27 Feb 2024 22:46:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <urlonm$3ep9p$2@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 27 Feb 2024 22:46:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d339ad4b21c64d41c58c7c072f1b4d54";
logging-data="3630393"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yB8dLFEBO3JOdcmmhR4RZ"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:6nPAAtbTWnfakNxl2YrUquCliSw=
View all headers

On Tue, 27 Feb 2024 13:36:25 -0800, Paul Rubin wrote:

> 1) Your "range" function already exists, called "iota" (after the iota
> operation in APL).

Just checked, and I don’t even need to import anything to use it. Thanks.

> 2) Similarly the "score" function looks translated from C to Scheme or
> something like that.

It was my attempt to translate this Python code:

def score(candidate, answer) :
return \
(
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)
)
)
#end score

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Wed, 28 Feb 2024 01:46 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Tue, 27 Feb 2024 17:46:48 -0800
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <871q8xe4xj.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="b11dea1ec4537807cb9ed89e3ac624c7";
logging-data="3696691"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++cmL+wmANrLM7xu72naqF"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:HCiKRhuzgwRcoxNmwhnX7imLLXY=
sha1:bg1VeQbSptq8ikMPwU8B7AzRw9M=
View all headers

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
> sum(a == b for a, b in zip(candidate, answer))

zip is in srfi-1 but I would write this as

(length (filter identity (map = candidate answer)))

> 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. In Scheme, maybe
this:

(define (score2 candidate answer)
(let* ((well-placed (length (filter identity (map = candidate answer))))
(wrongly-placed (- (length (lset-intersection = candidate answer))
well-placed)))
(values well-placed wrongly-placed)))

lset-intersection is also in srfi-1. Here, candidate and answer are
both lists of digits. I don't know the running time (complexity) of
lset-intersection. In Python, sets are represented with hashes so the
equivalent operation should take linear time. Scheme might uses hashes
(linear time), sorted lists (n log n time), or quadratic time.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Wed, 28 Feb 2024 21:57 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Wed, 28 Feb 2024 21:57:46 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uroa8p$3lu2$2@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 28 Feb 2024 21:57:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2b5f550de027bed53e7de3d0720ccb3e";
logging-data="120770"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/XxfawjPC4WfdqVX9tJ53T"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:ZjNm2IwqiAT5x7fvSPSDnaxQ3SE=
View all headers

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?

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: George Neuner
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: i2pn2 (i2pn.org)
Date: Wed, 28 Feb 2024 23:29 UTC
References: 1 2 3 4 5 6
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!weretis.net!feeder6.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: gneuner2@comcast.net (George Neuner)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Wed, 28 Feb 2024 18:29:50 -0500
Organization: i2pn2 (i2pn.org)
Message-ID: <49gvtitvd567m3u58j5qubarrh7qvdilml@4ax.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me> <87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me> <871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: i2pn2.org;
logging-data="282721"; mail-complaints-to="usenet@i2pn2.org";
posting-account="h5eMH71iFfocGZucc+SnA0y5I+72/ecoTCcIjMd3Uww";
User-Agent: ForteAgent/8.00.32.1272
X-Spam-Checker-Version: SpamAssassin 4.0.0
View all headers

On Wed, 28 Feb 2024 21:57:46 -0000 (UTC), Lawrence D'Oliveiro
<ldo@nz.invalid> wrote:

>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?

Because almost all of Python's standard libraries are written in C.
Most versions of Python are just too slow to use for anything but toy
programs.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Wed, 28 Feb 2024 23:54 UTC
References: 1 2 3 4 5 6
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Wed, 28 Feb 2024 15:54:48 -0800
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <8734tcta9j.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="b872400a71d39c8346220974146acd12";
logging-data="167539"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vkhu4coyWel3xoGBX99I6"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:vy3AkEqccuYcC06tSMp0/15MN0w=
sha1:J6Oio5+v5kNpJdT7rK80bk1A2pc=
View all headers

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>> 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?

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. But the idea is that sets in Python are
implemented with hashing, so finding a set intersection is takes O(N).

A purely functional approach (idk how lset in Guile works) might use
sorted lists for O(N log N) complexity, but either beats O(N**2).

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Thu, 29 Feb 2024 21:13 UTC
References: 1 2 3 4 5 6 7
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Thu, 29 Feb 2024 21:13:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <urqs1c$q9h9$2@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
<8734tcta9j.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 29 Feb 2024 21:13:17 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="daeb6559b2144face1d3464670b018f1";
logging-data="861737"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195QYxKf2xtFt8QewWSqxAP"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:bDwsfNvm+ds9Z/bsyHRiVIN27C4=
View all headers

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.

“Premature optimization is the root of all evil.”
-- variously attributed to Tony Hoare or Donald Knuth

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Thu, 29 Feb 2024 22:21 UTC
References: 1 2 3 4 5 6 7 8
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Thu, 29 Feb 2024 14:21:12 -0800
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <871q8ugbdz.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
<8734tcta9j.fsf@nightsong.com> <urqs1c$q9h9$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="b872400a71d39c8346220974146acd12";
logging-data="887701"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/XgaYyA54H7JSo3vsBmHKt"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:/YtCNiw+AY5xQX6yyOenNC49fOU=
sha1:NuIsfLNj3qhn2SY762voUL4ax6I=
View all headers

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
> “Premature optimization is the root of all evil.”

I would say using set intersections is clearer and more concise than
that code with loop indices too. It is what you were trying to compute
in the first place. Same idea as writing a matrix product as A*B
instead of as some messy thing with subscripts.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Thu, 29 Feb 2024 23:56 UTC
References: 1 2 3 4 5 6 7 8 9
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Thu, 29 Feb 2024 23:56:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <urr5ka$rvtd$7@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
<8734tcta9j.fsf@nightsong.com> <urqs1c$q9h9$2@dont-email.me>
<871q8ugbdz.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 29 Feb 2024 23:56:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="08fcff3ec016520633046216dcf0e434";
logging-data="917421"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hxGQ0/ANNIYNtvrLwkmUf"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:5smpgoFbtU3Hullo+hILEbPqZ3I=
View all headers

On Thu, 29 Feb 2024 14:21:12 -0800, Paul Rubin wrote:

> Same idea as writing a matrix product as A*B instead of as some messy
> thing with subscripts.

Somebody still has to write the underlying code with the subscripts.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Kaz Kylheku
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 00:24 UTC
References: 1 2 3 4 5 6 7 8
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Fri, 1 Mar 2024 00:24:22 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <20240229162403.123@kylheku.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
<8734tcta9j.fsf@nightsong.com> <urqs1c$q9h9$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 1 Mar 2024 00:24:22 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="24c55e5273c316a14a1eea66477d3f37";
logging-data="934164"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RD+wh5wExHpWqbhp9zGiVm5Azkj8HsO0="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:IgM1OZ11AAVbPuaym7h0RUY9bkg=
View all headers

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.
>
> “Premature optimization is the root of all evil.”
> -- variously attributed to Tony Hoare or Donald Knuth

Pinning it down more precisely at this stage would be premature
attribution.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 02:18 UTC
References: 1 2 3 4 5 6 7 8 9 10
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Thu, 29 Feb 2024 18:18:31 -0800
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <87wmqmelu0.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
<8734tcta9j.fsf@nightsong.com> <urqs1c$q9h9$2@dont-email.me>
<871q8ugbdz.fsf@nightsong.com> <urr5ka$rvtd$7@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="943bcaa197a75ee88da3147cd0703006";
logging-data="970899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/udWWzEWdSRxA1jHMzlcWc"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:w17yQ2dcQaMgvvlGGm/4YDSplN8=
sha1:bLUKfYH/Klo6T+cW0BJwBQjD1kE=
View all headers

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. It's normal to bang out code like that when you're trying
to keep moving, but refactoring afterwards generally helps. Here's a
refactored Python version of the score function I posted earlier:

def score(answer: int, candidate: int) -> Tuple[int,int]:
a = digits(answer)
b = digits(candidate)
well_placed = sum(x==y for x,y in zip(a,b))
wrongly_placed = len(set(a) & set(b)) - well_placed
return well_placed, wrongly_placed

Maybe it's more correct to use multisets (collections.Counter) instead
of sets, depending on how the problem is specified.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 03:16 UTC
References: 1 2 3 4 5 6 7 8 9 10 11
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Fri, 1 Mar 2024 03:16:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <urrhat$11rgn$3@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
<8734tcta9j.fsf@nightsong.com> <urqs1c$q9h9$2@dont-email.me>
<871q8ugbdz.fsf@nightsong.com> <urr5ka$rvtd$7@dont-email.me>
<87wmqmelu0.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 1 Mar 2024 03:16:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="08fcff3ec016520633046216dcf0e434";
logging-data="1109527"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/g/cEW+v2FJ5XyRTvFV/IB"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:jlcOgHKcl7yTpVEA9PlgHRED7S4=
View all headers

On Thu, 29 Feb 2024 18:18:31 -0800, Paul Rubin wrote:

> 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 I’m going to reuse
them. Splitting things off just for the sake of doing so is what we could
call a “fragmentation smell” or a “gratuitous hierarchy antipattern”.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme, comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 03:35 UTC
References: 1 2 3 4 5 6 7 8 9 10 11 12
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Thu, 29 Feb 2024 19:35:30 -0800
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <87le72ei9p.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <urlonm$3ep9p$2@dont-email.me>
<871q8xe4xj.fsf@nightsong.com> <uroa8p$3lu2$2@dont-email.me>
<8734tcta9j.fsf@nightsong.com> <urqs1c$q9h9$2@dont-email.me>
<871q8ugbdz.fsf@nightsong.com> <urr5ka$rvtd$7@dont-email.me>
<87wmqmelu0.fsf@nightsong.com> <urrhat$11rgn$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="943bcaa197a75ee88da3147cd0703006";
logging-data="1116582"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190nkHXPWmG76YJerLd2xjt"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:xjJXbawuwjGAMnGostvz5ni/8f4=
sha1:fp3QK2dGIxDIOuGzSUNYM1J3P5g=
View all headers

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
> I only push things into separate/library functions if I’m going to reuse
> them. Splitting things off just for the sake of doing so is what we could
> call a “fragmentation smell” or a “gratuitous hierarchy antipattern”.

In the case of matrix multiplication, the code is already in a linear
algebra library. In the case of set intersection, it is built into
Python's set type.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Andreas Eder
Newsgroups: comp.lang.scheme, comp.lang.lisp
Followup: comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 10:50 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: a_eder_muc@web.de (Andreas Eder)
Newsgroups: comp.lang.scheme,comp.lang.lisp
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Followup-To: comp.lang.scheme
Date: Fri, 01 Mar 2024 11:50:36 +0100
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <87zfvi5ipv.fsf@eder.anydns.info>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="f98840aacfbedad24b3dd4823636d70d";
logging-data="1270418"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197RPA+B8AOseVpCaoCvgb1"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:4lSoPR+gRsaJKrlt0iGY2Gu03aI=
sha1:/fz9ypnxdENwEduu79CkBRxjkec=
View all headers

On Di 27 Feb 2024 at 13:36, Paul Rubin <no.email@nospam.invalid> wrote:

> 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 '()))
(if (< n 0)
a
(go (1- n) (cons n a)))))

'Andreas

--
ceterum censeo redmondinem esse delendam

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 11:56 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Fri, 01 Mar 2024 03:56:08 -0800
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <87cysedv3b.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <87zfvi5ipv.fsf@eder.anydns.info>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="af78deefa5fc256c2b93007bcaf6a8df";
logging-data="1292150"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Rq4zoxUzCHBGIprRzL8iG"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:Qz3mdg4tcZop9+5fhJQfHU8u+qU=
sha1:/lmzv4tvZvJg66gC5Lp88WG5XJs=
View all headers

Andreas Eder <a_eder_muc@web.de> writes:
> I would write it without the second define using a named let:
>
> (define (range n)
> (let go ((n n) (a '())) ...

Oh interesting, I didn't know that syntax. I will check the docs. Thanks.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Lawrence D'Oliv
Newsgroups: comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 21:08 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.scheme
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code
Sought
Date: Fri, 1 Mar 2024 21:08:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <urtg3s$1egu1$1@dont-email.me>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <87zfvi5ipv.fsf@eder.anydns.info>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 1 Mar 2024 21:08:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="08fcff3ec016520633046216dcf0e434";
logging-data="1524673"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19aXCPA2Fu7uP6zP5mr37bq"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:Rh3jYeVkHpxbyHp6lOZZ6XPGfm8=
View all headers

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.

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: George Neuner
Newsgroups: comp.lang.scheme
Organization: i2pn2 (i2pn.org)
Date: Fri, 1 Mar 2024 21:53 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!weretis.net!feeder6.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: gneuner2@comcast.net (George Neuner)
Newsgroups: comp.lang.scheme
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Fri, 01 Mar 2024 16:53:49 -0500
Organization: i2pn2 (i2pn.org)
Message-ID: <3hj4uite1hn3eemhlmt7sga5o0rdu66hl4@4ax.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me> <87edcxegiu.fsf@nightsong.com> <87zfvi5ipv.fsf@eder.anydns.info> <urtg3s$1egu1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="505605"; mail-complaints-to="usenet@i2pn2.org";
posting-account="h5eMH71iFfocGZucc+SnA0y5I+72/ecoTCcIjMd3Uww";
User-Agent: ForteAgent/8.00.32.1272
X-Spam-Checker-Version: SpamAssassin 4.0.0
View all headers

On Fri, 1 Mar 2024 21:08:13 -0000 (UTC), Lawrence D'Oliveiro
<ldo@nz.invalid> wrote:

>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

Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
From: Paul Rubin
Newsgroups: comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 1 Mar 2024 22:24 UTC
References: 1 2 3 4 5 6
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.scheme
Subject: Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Sought
Date: Fri, 01 Mar 2024 14:24:37 -0800
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <874jdpegka.fsf@nightsong.com>
References: <urh3vu$2br0h$1@dont-email.me> <urjq6t$31cu6$1@dont-email.me>
<87edcxegiu.fsf@nightsong.com> <87zfvi5ipv.fsf@eder.anydns.info>
<urtg3s$1egu1$1@dont-email.me>
<3hj4uite1hn3eemhlmt7sga5o0rdu66hl4@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="af78deefa5fc256c2b93007bcaf6a8df";
logging-data="1558534"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+lZHdXMpwm7UHKjIk0bwYc"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:xBwznWQZwfpC2UAhZkBXi2+1FSQ=
sha1:l5qY156nMsQDn1YYD43wxJ4DEOI=
View all headers

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.

Pages:12

rocksolid light 0.9.8
clearnet tor