added a small template based word generator

This commit is contained in:
opfez 2022-10-26 10:29:41 +02:00
parent 21ab12cc9c
commit fea699358b
1 changed files with 39 additions and 0 deletions

39
wordgen.scm Normal file
View File

@ -0,0 +1,39 @@
;; A word generator designed for use in an interactive environment
(define (fold-right f init seq)
(if (null? seq)
init
(f (car seq)
(fold-right f init (cdr seq)))))
(define (pick-random lst)
(let ((index (random (length lst))))
(list-ref lst index)))
(define (C)
(pick-random '("s" "v" "ch" "j")))
(define (V)
(pick-random '("a" "e" "u")))
(define (N)
(pick-random '("n" "l")))
(define (string->procs str)
(define (randomize chars)
(cond ((null? chars) '())
((char=? (car chars) #\?)
(if (zero? (random 2))
(cons (cadr chars) (randomize (cddr chars)))
(randomize (cddr chars))))
(else (cons (car chars) (randomize (cdr chars))))))
(map (lambda (c) (eval (string->symbol (string c))))
(randomize (string->list str))))
(define (generate-word template)
(apply string-append
(map (lambda (f) (f))
(string->procs template))))
;; Example: (generate-word "CVNVN")
;; => "jenel"