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.
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)))
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)
[ 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)
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 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)
...
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.]
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").
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.
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.
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.
[*] 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.
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
R5RS already.
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
R5RS already.
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 415 |
Nodes: | 16 (2 / 14) |
Uptime: | 166:37:38 |
Calls: | 8,708 |
Calls today: | 2 |
Files: | 13,270 |
Messages: | 5,952,229 |