XPost: comp.lang.scheme
Eric Smith wrote:
; (rm1 "abc" 0) ==> "bc" (rm1 "tea" 1) ==> "ta"
(defun rm1 (seq which)
(remove-if #'true seq :start which :end (1+ which)))
; (consword #\a "bc") ==> "abc"
(defun consword (char word)
(concatenate 'string (string char) word))
; (anagrams "ah") ==> ("ah" "ha")
(defun anagrams (word)
(if (= (length word) 1) (list word)
(loop as x across word
as i upfrom 0
as subword = (rm1 word i)
nconc (loop as y in (anagrams subword)
collect (consword x y)))))
Gauche Scheme
(use util.combinations) ;; permutations
(define (anagrams word)
;; Clojure-style threading or pipelining.
(->> word string->list permutations (map list->string)))
(anagrams "try")
===>
("try" "tyr" "rty" "ryt" "ytr" "yrt")
Given:
(define-syntax ->>
(syntax-rules ()
[(_ x) x]
[(_ x (y ...) z ...)
(->> (y ... x) z ...)]
[(_ x y z ...)
(->> (y x) z ...)]))
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)