• string chains

    From B. Pym@21:1/5 to All on Mon Aug 5 23:34:32 2024
    The task is to somehow implement Amb, and demonstrate it with
    a program which chooses one word from each of the following
    four sets of character strings to generate a four-word
    sentence:

    "the" "that" "a"
    "frog" "elephant" "thing"
    "walked" "treaded" "grows"
    "slowly" "quickly"

    The constraint to be satisfied is that the last character of
    each word (other than the last) is the same as the first
    character of its successor.

    The only successful sentence is "that thing grows slowly";
    other combinations do not satisfy the constraint and thus
    fail.

    newLISP

    (define (cartesian-product lists)
    (if (null? lists)
    '(())
    (let (subproduct (cartesian-product (rest lists)))
    (apply append
    (map
    (fn (x) (map (fn (xs) (cons x xs)) subproduct))
    (first lists))))))

    (define (good? xs)
    (for-all
    (fn (pair) (starts-with (pair 1) ((pair 0) -1)))
    (map list (0 -1 xs) (rest xs))))

    (filter good?
    (cartesian-product
    '(("preconize" "cozy" "Lilliputian")
    ("climb" "nub" "snob" "end" "yet")
    ("however" "by" "but" "so" "tot")
    ("the" "that" "a" "tack" "of")
    ("frog" "elephant" "thing")
    ("walked" "treaded" "grows")
    ("slowly" "quickly")
    ("yank" "can" "you" "choose")
    ("won't" "understand"))))

    (("cozy" "yet" "tot" "that" "thing" "grows" "slowly" "you"
    "understand")
    ("Lilliputian" "nub" "but" "that" "thing" "grows" "slowly"
    "you" "understand"))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to B. Pym on Tue Aug 6 10:02:30 2024
    On 8/5/2024 4:34 PM, B. Pym wrote:
    The task is to somehow implement Amb, and demonstrate it with
    a program which chooses one word from each of the following
    four sets of character strings to generate a four-word
    sentence:

    "the" "that" "a"
    "frog" "elephant" "thing"
    "walked" "treaded" "grows"
    "slowly" "quickly"

    The constraint to be satisfied is that the last character of
    each word (other than the last) is the same as the first
    character of its successor.

    The only successful sentence is "that thing grows slowly";
    other combinations do not satisfy the constraint and thus
    fail.

    newLISP

    (define (cartesian-product lists)
    (if (null? lists)
    '(())
    (let (subproduct (cartesian-product (rest lists)))
    (apply append
    (map
    (fn (x) (map (fn (xs) (cons x xs)) subproduct))
    (first lists))))))

    (define (good? xs)
    (for-all
    (fn (pair) (starts-with (pair 1) ((pair 0) -1)))
    (map list (0 -1 xs) (rest xs))))

    (filter good?
    (cartesian-product
    '(("preconize" "cozy" "Lilliputian")
    ("climb" "nub" "snob" "end" "yet")
    ("however" "by" "but" "so" "tot")
    ("the" "that" "a" "tack" "of")
    ("frog" "elephant" "thing")
    ("walked" "treaded" "grows")
    ("slowly" "quickly")
    ("yank" "can" "you" "choose")
    ("won't" "understand"))))

    (("cozy" "yet" "tot" "that" "thing" "grows" "slowly" "you"
    "understand")
    ("Lilliputian" "nub" "but" "that" "thing" "grows" "slowly"
    "you" "understand"))


    Did you avoid using Scheme because it's bad for this
    type of string manipulation?

    ----- e.g. in Python the last char of string is just String[-1]



    >>> May 12, 2019 — newLISP is a general purpose scripting
    language for developing web applications and programs in general in the
    domains of artificial ...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to HenHanna on Wed Aug 7 03:51:20 2024
    On 2024-08-06, HenHanna <HenHanna@devnull.tb> wrote:
    On 8/5/2024 4:34 PM, B. Pym wrote:
    The task is to somehow implement Amb, and demonstrate it with
    a program which chooses one word from each of the following
    four sets of character strings to generate a four-word
    sentence:

    "the" "that" "a"
    "frog" "elephant" "thing"
    "walked" "treaded" "grows"
    "slowly" "quickly"

    The constraint to be satisfied is that the last character of
    each word (other than the last) is the same as the first
    character of its successor.

    The only successful sentence is "that thing grows slowly";
    other combinations do not satisfy the constraint and thus
    fail.

    newLISP

    (define (cartesian-product lists)
    (if (null? lists)
    '(())
    (let (subproduct (cartesian-product (rest lists)))
    (apply append
    (map
    (fn (x) (map (fn (xs) (cons x xs)) subproduct))
    (first lists))))))

    (define (good? xs)
    (for-all
    (fn (pair) (starts-with (pair 1) ((pair 0) -1)))
    (map list (0 -1 xs) (rest xs))))

    (filter good?
    (cartesian-product
    '(("preconize" "cozy" "Lilliputian")
    ("climb" "nub" "snob" "end" "yet")
    ("however" "by" "but" "so" "tot")
    ("the" "that" "a" "tack" "of")
    ("frog" "elephant" "thing")
    ("walked" "treaded" "grows")
    ("slowly" "quickly")
    ("yank" "can" "you" "choose")
    ("won't" "understand"))))

    (("cozy" "yet" "tot" "that" "thing" "grows" "slowly" "you"
    "understand")
    ("Lilliputian" "nub" "but" "that" "thing" "grows" "slowly"
    "you" "understand"))


    Did you avoid using Scheme because it's bad for this
    type of string manipulation?

    There isn't a lot of string manipulation here; just checking
    that the last character of a string is or isn't the same
    as the first character of another.


    (defun is-shiritori (wlist)
    (if-match @(scan @(require (@w1 @w2 . @nil)
    (or (empty w1)
    (empty w2)
    (neq [w1 -1] [w2 0]))))
    wlist
    nil t))
    is-shiritori
    [apply maprend [chain list [iff is-shiritori list]]
    '(("preconize" "cozy" "Lilliputian")
    ("climb" "nub" "snob" "end" "yet")
    ("however" "by" "but" "so" "tot")
    ("the" "that" "a" "tack" "of")
    ("frog" "elephant" "thing")
    ("walked" "treaded" "grows")
    ("slowly" "quickly")
    ("yank" "can" "you" "choose")
    ("won't" "understand"))]
    (("cozy" "yet" "tot" "that" "thing" "grows" "slowly" "you" "understand")
    ("Lilliputian" "nub" "but" "that" "thing" "grows" "slowly" "you"
    "understand"))

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