fork(1) download
  1. ; scrambled words
  2.  
  3. (define (shuffle x)
  4. (do ((v (list->vector x)) (n (length x) (- n 1)))
  5. ((zero? n) (vector->list v))
  6. (let* ((r (random n)) (t (vector-ref v r)))
  7. (vector-set! v r (vector-ref v (- n 1)))
  8. (vector-set! v (- n 1) t))))
  9.  
  10. (define (scramble str)
  11. (let* ((cs (string->list str))
  12. (upper (map char-upper-case? cs))
  13. (cs (map char-downcase cs)))
  14. (let loop ((cs cs) (word (list)) (zs (list)))
  15. (cond ((null? cs) ; end of input
  16. (list->string
  17. (map (lambda (u? c)
  18. (if u? (char-upcase c) c))
  19. upper (reverse zs))))
  20. ((char-alphabetic? (car cs)) ; in a word
  21. (loop (cdr cs) (cons (car cs) word) zs))
  22. ((pair? word) ; end of word
  23. (loop cs (list) (append (shuffle word) zs)))
  24. (else ; not in a word
  25. (loop (cdr cs) word (cons (car cs) zs)))))))
  26.  
  27. (display (scramble "Programming Praxis is fun!")) (newline)
  28. (display (scramble "Programming Praxis is fun!")) (newline)
  29. (display (scramble "Programming Praxis is fun!")) (newline)
Success #stdin #stdout 0s 7908KB
stdin
Standard input is empty
stdout
Ngomripmagr Sxirap is ufn!
Pgamrroigmn Rapsix si unf!
Rongarpmgim Apixrs is unf!