fork 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. ((and ; collect letter into accumulator
  21. (pair? zs)
  22. (char-alphabetic? (car zs))
  23. (char-alphabetic? (car cs))
  24. (pair? (cdr cs))
  25. (char-alphabetic? (cadr cs)))
  26. (loop (cdr cs) (cons (car cs) word) zs))
  27. ((pair? word) ; end of word interior
  28. (loop (cddr cs) (list)
  29. (append (list (cadr cs))
  30. (list (car cs))
  31. (shuffle word) zs)))
  32. (else ; not in a word
  33. (loop (cdr cs) word (cons (car cs) zs)))))))
  34.  
  35. (display (scramble "Programming Praxis is fun!")) (newline)
  36. (display (scramble "Programming Praxis is fun!")) (newline)
  37. (display (scramble "Programming Praxis is fun!")) (newline)
Success #stdin #stdout 0.01s 8120KB
stdin
Standard input is empty
stdout
Pramigomrng Praixs is fun!
Pioamrgnrmg Prxias is fun!
Pgnrmmiroag Paixrs is fun!