Re: function to split a string into a list of characters

Liste des GroupesRevenir à cl lisp 
Sujet : Re: function to split a string into a list of characters
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 13. Sep 2024, 23:50:55
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vc2c3r$1244m$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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 ...)]))

Date Sujet#  Auteur
13 Sep23:50 o Re: function to split a string into a list of characters1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal