Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp Subject: string chains Date: Mon, 5 Aug 2024 23:34:32 -0000 (UTC) Organization: A noiseless patient Spider Lines: 50 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Tue, 06 Aug 2024 01:34:32 +0200 (CEST) Injection-Info: dont-email.me; posting-host="51ec0738d78fae23c740e6e73e04a0c7"; logging-data="1236648"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195vPZLsLLv5L3Xl7tGETe8" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:nuR71LXQe71X2xqE27ZwpBslZDs= Bytes: 2364 > 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"))