• Re: (Mastermind) puzzle (with 3 digits) -- Elegant (readable) code Soug

    From Paul Rubin@21:1/5 to Paul Rubin on Tue Feb 27 13:46:57 2024
    XPost: comp.lang.lisp

    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) '()))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Eder@21:1/5 to Paul Rubin on Fri Mar 1 11:50:36 2024
    XPost: comp.lang.lisp

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Andreas Eder on Fri Mar 1 03:56:08 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Andreas Eder on Fri Mar 1 21:08:13 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Neuner@21:1/5 to ldo@nz.invalid on Fri Mar 1 16:53:49 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to George Neuner on Fri Mar 1 14:24:37 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Neuner@21:1/5 to no.email@nospam.invalid on Fri Mar 1 22:52:54 2024
    On Fri, 01 Mar 2024 14:24:37 -0800, Paul Rubin
    <no.email@nospam.invalid> wrote:

    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.

    Named Let was formally added to Scheme in R5RS, but it was recognized
    as being a legal variant at least as far back as R3RS.


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

    R3RS [4.2.4 Iteration]:
    R4RS [4.2.4 Iteration]:

    :

    (let <variable> <bindings> <body>) syntax

    Some implementations of Scheme permit a variant on the
    syntax of let called \named let" which provides a more
    general looping construct than do, and may also be used
    to express recursions.

    Named let has the same syntax and semantics as ordinary
    let except that <variable> is bound within <body> to
    a procedure whose formal arguments are the bound vari-
    ables and whose body is <body>. Thus the execution of
    hbodyi may be repeated by invoking the procedure named
    by <variable>.

    (let loop ((numbers '(3 -2 1 6 -5))
    (nonneg '())
    (neg '()))
    (cond ((null? numbers) (list nonneg neg))
    ((>= (car numbers) 0)
    (loop (cdr numbers)
    (cons (car numbers) nonneg)
    neg))
    ((< (car numbers) 0)
    (loop (cdr numbers)
    nonneg
    (cons (car numbers) neg)))))
    ==> ((6 1 3) (-5 -2))

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

    R5RS [4.2.4 Iteration]:

    :

    (let <variable> <bindings> <body>) library syntax

    “Named let” is a variant on the syntax of let which pro-
    vides a more general looping construct than do and may
    also be used to express recursions. It has the same syn-
    tax and semantics as ordinary let except that <variable>
    is bound within <body> to a procedure whose formal argu-
    ments are the bound variables and whose body is <body>.
    Thus the execution of <body> may be repeated by invoking
    the procedure named by <variable>.

    :

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



    Unfortunately I don't have any references older than R3RS.

    George

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to Lawrence D'Oliveiro on Mon Mar 4 10:30:26 2024
    XPost: comp.lang.lisp

    Lawrence D'Oliveiro wrote:

    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 love it.... i thought it must be THE most enigmatic of his quotes, but...



    https://www.cs.yale.edu/homes/perlis-alan/quotes.html


    38. Structured Programming supports the law of the excluded middle. --------- ??????????

    39. Re graphics: A picture is worth 10K words - but only those to describe the picture. Hardly any sets of 10K words can be adequately described with pictures.

    40. There are two ways to write error-free programs; only the third one works.

    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.

    43. In software systems, it is often the early bird that makes the worm. ---------- meaning, ...that introduces the BUG ?

    44.Sometimes I think the only universal in the computing field is the fetch-execute cycle.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Andreas Eder on Sun May 26 08:11:11 2024
    On 3/1/2024, Andreas Eder wrote:

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

    Looks good.

    Using "do":

    (define (range n)
    (do ((i n (- i 1))
    (a '() (cons i a)))
    ((< i 0) a)))


    Using "unfold-right" in Gauche Scheme:

    (use srfi-1)

    Gauche doesn't have "1-".

    (define (1- n) (- n 1))

    (define (range n) (unfold-right negative? values 1- n))

    (range 9)
    ===>
    (0 1 2 3 4 5 6 7 8 9)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)