• #### Re: X in every language syndrome

From B. Pym@21:1/5 to All on Sat Jul 6 21:58:58 2024
The question is about so-called "bellied"
numbers, defined as 4-digit integers for which the sum of the two
"middle" digits is smaller than the sum of the two outer digits. So 1265
is bellied, while 4247 is not.

[ He means the sum of middle digits is larger. ]

This checking part is easy:

(defun bellied-number-p (n)
(> (+ (mod (truncate n 10) 10) (mod (truncate n 100) 10))
(+ (mod n 10) (truncate n 1000))))

Now the task is to find the longest, uninterrupted sequence of bellied numbers within all 4-digit number, hence from 1000 to 9999. And this is
where I terribly screwed up:

While the following code does the job,

(let ((max-length 0)
(current-length 0)
(last-bellied-number 0))
(dotimes (m 9000)
(let ((n (+ 1000 m)))
(if (bellied-number-p n)
(incf current-length)
(progn
(when (> current-length max-length)
(setf max-length current-length)
(setf last-bellied-number (1- n)))
(setf current-length 0)))))
(print (format t "~&Longest sequence of ~a bellied numbers ends at ~a."
max-length last-bellied-number)))

[ Another poster: ]

TXR Lisp.

Having defined:

(defun bellied-p (num)
(let ((d (digits num)))
(and (= 4 (len d))
(< (+ [d 0] [d 3])
(+ [d 1] [d 2])))))

We casually do this at the prompt:

1> [find-max [partition-by bellied-p (range 1000 9999)] :
[iff [chain car bellied-p] len (ret 0)]]
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
1998 1999)

Shorter.

Gauche Scheme:

(use gauche.sequence) ;; group-contiguous-sequence find-max
,print-mode pretty #t length #f width 64

(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))

(find-max
(group-contiguous-sequence (filter bellied? (iota 9000 1000)))
:key length)

===>
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
1992 1993 1994 1995 1996 1997 1998 1999)

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Kaz Kylheku@21:1/5 to B. Pym on Sun Jul 7 05:58:57 2024
On 2024-07-06, B. Pym <No_spamming@noWhere_7073.org> wrote:
[ Another poster: ]

TXR Lisp.

Having defined:

(defun bellied-p (num)
(let ((d (digits num)))
(and (= 4 (len d))
(< (+ [d 0] [d 3])
(+ [d 1] [d 2])))))

We casually do this at the prompt:

1> [find-max [partition-by bellied-p (range 1000 9999)] :
[iff [chain car bellied-p] len (ret 0)]]
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
1998 1999)

Shorter.

Gauche Scheme:

(use gauche.sequence) ;; group-contiguous-sequence find-max
,print-mode pretty #t length #f width 64

(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))

(find-max
(group-contiguous-sequence (filter bellied? (iota 9000 1000)))
:key length)

(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
1992 1993 1994 1995 1996 1997 1998 1999)

(defun bellied (num)
(match (@a @b @c @d) (digits num)
(< (+ a d) (+ b c))))

[find-max (split* (range 1000 9999) (op where [notf bellied])) : len]

OR:

(defun not-bellied (num)
(match (@a @b @c @d) (digits num)
(>= (+ a d) (+ b c))))

[find-max (split* (range 1000 9990) (op where not-bellied)) : len]

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

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From HenHanna@21:1/5 to B. Pym on Sat Jul 6 22:21:06 2024
On 7/6/2024 2:58 PM, B. Pym wrote:
The question is about so-called "bellied"
numbers, defined as 4-digit integers for which the sum of the two
"middle" digits is smaller than the sum of the two outer digits. So 1265
is bellied, while 4247 is not.

[ He means the sum of middle digits is larger. ]

This checking part is easy:

(defun bellied-number-p (n)
(> (+ (mod (truncate n 10) 10) (mod (truncate n 100) 10))
(+ (mod n 10) (truncate n 1000))))

Now the task is to find the longest, uninterrupted sequence of bellied
numbers within all 4-digit number, hence from 1000 to 9999. And this is
where I terribly screwed up:

While the following code does the job,

(let ((max-length 0)
(current-length 0)
(last-bellied-number 0))
(dotimes (m 9000)
(let ((n (+ 1000 m)))
(if (bellied-number-p n)
(incf current-length)
(progn
(when (> current-length max-length)
(setf max-length current-length)
(setf last-bellied-number (1- n)))
(setf current-length 0)))))
(print (format t "~&Longest sequence of ~a bellied numbers ends at ~a." >> max-length last-bellied-number)))

[ Another poster: ]

TXR Lisp.

Having defined:

(defun bellied-p (num)
(let ((d (digits num)))
(and (= 4 (len d))
(< (+ [d 0] [d 3])
(+ [d 1] [d 2])))))

We casually do this at the prompt:

1> [find-max [partition-by bellied-p (range 1000 9999)] :
[iff [chain car bellied-p] len (ret 0)]]
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
1998 1999)

Shorter.

Gauche Scheme:

(use gauche.sequence) ;; group-contiguous-sequence find-max
,print-mode pretty #t length #f width 64

(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))

(find-max
(group-contiguous-sequence (filter bellied? (iota 9000 1000)))
:key length)

===>
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
1992 1993 1994 1995 1996 1997 1998 1999)

,print-mode pretty #t length #f width 64 <--- What does this line do?

______________________

the Opposite of Humped must be Hourglass (shaped)

(9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013
9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027
9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041
9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055
9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069
9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083
9084 9085 9086 9087 9088 9089)
#t
gosh>

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From B. Pym@21:1/5 to B. Pym on Sun Jul 7 19:42:18 2024
On 7/6/2024, B. Pym wrote:

The question is about so-called "bellied"
numbers, defined as 4-digit integers for which the sum of the two
"middle" digits is smaller than the sum of the two outer digits. So 1265
is bellied, while 4247 is not.

[ He means the sum of middle digits is larger. ]

This checking part is easy:

(defun bellied-number-p (n)
(> (+ (mod (truncate n 10) 10) (mod (truncate n 100) 10))
(+ (mod n 10) (truncate n 1000))))

Now the task is to find the longest, uninterrupted sequence of bellied numbers within all 4-digit number, hence from 1000 to 9999. And this is where I terribly screwed up:

While the following code does the job,

(let ((max-length 0)
(current-length 0)
(last-bellied-number 0))
(dotimes (m 9000)
(let ((n (+ 1000 m)))
(if (bellied-number-p n)
(incf current-length)
(progn
(when (> current-length max-length)
(setf max-length current-length)
(setf last-bellied-number (1- n)))
(setf current-length 0)))))
(print (format t "~&Longest sequence of ~a bellied numbers ends at ~a."
max-length last-bellied-number)))

[ Another poster: ]

TXR Lisp.

Having defined:

(defun bellied-p (num)
(let ((d (digits num)))
(and (= 4 (len d))
(< (+ [d 0] [d 3])
(+ [d 1] [d 2])))))

We casually do this at the prompt:

1> [find-max [partition-by bellied-p (range 1000 9999)] :
[iff [chain car bellied-p] len (ret 0)]]
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
1998 1999)

Shorter.

Gauche Scheme:

(use gauche.sequence) ;; group-contiguous-sequence find-max
,print-mode pretty #t length #f width 64

(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))

(find-max
(group-contiguous-sequence (filter bellied? (iota 9000 1000)))
:key length)

===>
(1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
1992 1993 1994 1995 1996 1997 1998 1999)

A lower-level way.

(define (bellied? n)
(define (d i) (mod (div n (expt 10 i)) 10))
(> (+ (d 1) (d 2))
(+ (d 0) (d 3))))

(define (calc-length i)
(do ((j i (+ j 1)))
((not (bellied? j)) (- j i))))

(define (longest-bellied-seq)
(let go ((i 1000) (start 0) (len 0))
(if (> i 9999)
(list start len)
(let ((new-len (calc-length i)))
(cond ((zero? new-len) (go (+ i 1) start len))
((> new-len len) (go (+ i new-len) i new-len))
(#t (go (+ i new-len) start len)))))))

===>
(1920 80)

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From George Neuner@21:1/5 to All on Mon Jul 8 12:13:54 2024
On Sun, 24 May 2020 00:30:21 -0600, Jeff Barnett <jbb@notatt.com>
wrote:

...
By the way, these "use" directives are about as low
level as you can get. Why doesn't the system load whatever is used? Is
your toy designed for less then megabyte memory machines. [There's two >questions buried in here.]

Stupid question: why do you consider Scheme using a library to be more
"low level" than Lisp using a package?

Gauche is a bit odd in that it has both "use" and "require", but the
difference is only syntax. Gauche has a culture of distinguishing use
of the built-in extensions ("use") vs other libraries ("require").

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Kaz Kylheku@21:1/5 to George Neuner on Tue Jul 9 02:18:23 2024
On 2024-07-08, George Neuner <gneuner2@comcast.net> wrote:
On Sun, 24 May 2020 00:30:21 -0600, Jeff Barnett <jbb@notatt.com>
wrote:

...
By the way, these "use" directives are about as low
level as you can get. Why doesn't the system load whatever is used? Is
your toy designed for less then megabyte memory machines. [There's two >>questions buried in here.]

Stupid question: why do you consider Scheme using a library to be more
"low level" than Lisp using a package?

Gauche is a bit odd in that it has both "use" and "require", but the difference is only syntax. Gauche has a culture of distinguishing use
of the built-in extensions ("use") vs other libraries ("require").

The built-in stuff should not require any magic incantation to come
online. If such a thing is needed in something calling itself a Lisp
dialect, that's embarrassing.

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

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From George Neuner@21:1/5 to 643-408-1753@kylheku.com on Wed Jul 10 13:00:58 2024
On Tue, 9 Jul 2024 02:18:23 -0000 (UTC), Kaz Kylheku
<643-408-1753@kylheku.com> wrote:

On 2024-07-08, George Neuner <gneuner2@comcast.net> wrote:
On Sun, 24 May 2020 00:30:21 -0600, Jeff Barnett <jbb@notatt.com>
wrote:

...
By the way, these "use" directives are about as low
level as you can get. Why doesn't the system load whatever is used? Is >>>your toy designed for less then megabyte memory machines. [There's two >>>questions buried in here.]

Stupid question: why do you consider Scheme using a library to be more
"low level" than Lisp using a package?

Gauche is a bit odd in that it has both "use" and "require", but the
difference is only syntax. Gauche has a culture of distinguishing use
of the built-in extensions ("use") vs other libraries ("require").

The built-in stuff should not require any magic incantation to come
online. If such a thing is needed in something calling itself a Lisp
dialect, that's embarrassing.

Clearly Scheme is a Lisp *derivative*, but I think it's debatable
whether Scheme really can be considered a Lisp *dialect*. Its
semantics are very different and it was as much inspired by Algol as
by Lisp.

YMMV.

In any case, in that code fragment, the functions provided by
util.match are *not* built-in - they are in an implementation specific
library.

Prior to R6RS (circa 2007), Scheme did not even have syntax for
specifying or using libraries. Although a number of implementations
did provide them, there was no "standard" way to do it. Gauche's
"extensions" fall into this category.

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Paul Rubin@21:1/5 to George Neuner on Thu Jul 11 01:45:39 2024
George Neuner <gneuner2@comcast.net> writes:
Prior to R6RS (circa 2007), Scheme did not even have syntax for
specifying or using libraries. Although a number of implementations
did provide them, there was no "standard" way to do it.

That is an intentional practice with Scheme, I believe. They don't like
to standardize stuff until there are implementations out there that
users seem to think are done right.

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From George Neuner@21:1/5 to no.email@nospam.invalid on Fri Jul 12 07:19:47 2024
On Thu, 11 Jul 2024 01:45:39 -0700, Paul Rubin
<no.email@nospam.invalid> wrote:

George Neuner <gneuner2@comcast.net> writes:
Prior to R6RS (circa 2007), Scheme did not even have syntax for
specifying or using libraries. Although a number of implementations
did provide them, there was no "standard" way to do it.

That is an intentional practice with Scheme, I believe. They don't like
to standardize stuff until there are implementations out there that
users seem to think are done right.

R_RS documents are not standards per se - they simply describe what is considered a conforming implementation.

There are ISO and ANSI standards for Scheme, but they are based on
R3RS, circa ~1990[*]. As far as I'm aware, no other version of scheme
has been the subject of a standard.

[*] The R3RS document is not dated (that I can find), but I was using
an R3RS implementation in grad school in the early 90's.

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From George Neuner@21:1/5 to gneuner2@comcast.net on Fri Jul 12 09:44:50 2024
On Fri, 12 Jul 2024 07:19:47 -0400, George Neuner
<gneuner2@comcast.net> wrote:

[*] The R3RS document is not dated (that I can find), but I was using
an R3RS implementation in grad school in the early 90's.

I think by the time I was leaving grad school in '93 they were up to

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Nils M Holm@21:1/5 to George Neuner on Fri Jul 12 15:37:33 2024
George Neuner <gneuner2@comcast.net> wrote:
On Fri, 12 Jul 2024 07:19:47 -0400, George Neuner
<gneuner2@comcast.net> wrote:

[*] The R3RS document is not dated (that I can find), but I was using
an R3RS implementation in grad school in the early 90's.

I think by the time I was leaving grad school in '93 they were up to

Interesting, the R3RS indeed has no date. The RRRS is dated 1985 and
the R4RS 1991. In think R3RS may have been 1986 or 1987. FWIW, I see
no date later than 1986 in the references. Then there was a long break
between R4 and R5. R5RS appeared in 1998.

--
Nils M Holm < n m h @ t 3 x . o r g > http://t3x.org

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From George Neuner@21:1/5 to Nils M Holm on Sun Jul 14 11:53:16 2024
On 12 Jul 2024 15:37:33 GMT, Nils M Holm <nmh@sraddha.invalid> wrote:

George Neuner <gneuner2@comcast.net> wrote:
On Fri, 12 Jul 2024 07:19:47 -0400, George Neuner
<gneuner2@comcast.net> wrote:

[*] The R3RS document is not dated (that I can find), but I was using
an R3RS implementation in grad school in the early 90's.

I think by the time I was leaving grad school in '93 they were up to

Interesting, the R3RS indeed has no date. The RRRS is dated 1985 and
the R4RS 1991. In think R3RS may have been 1986 or 1987. FWIW, I see
no date later than 1986 in the references. Then there was a long break >between R4 and R5. R5RS appeared in 1998.

I defer to your knowledge of the history. 8-)

I thought it was an R5 I was using because it had syntax-rules, but
re-reading the R_RS docs, I see that R4 could have had them as an
extension.

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Nils M Holm@21:1/5 to George Neuner on Mon Jul 15 19:17:28 2024
George Neuner <gneuner2@comcast.net> wrote:
I defer to your knowledge of the history. 8-)

8)

I thought it was an R5 I was using because it had syntax-rules, but re-reading the R_RS docs, I see that R4 could have had them as an
extension.

R4RS described define-syntax, syntax-rules, etc in an appendix. I think
the system was pretty much identical to what became "official" in R5RS,
but I am not entirely sure, because I have always prefered the low-level,
LISP defmacro-style macros, which many R4RS implementations also offered.

--
Nils M Holm < n m h @ t 3 x . o r g > http://t3x.org

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