fork download
  1. ; changing gender
  2.  
  3. (define (all? pred? xs)
  4. (cond ((null? xs) #t)
  5. ((pred? (car xs))
  6. (all? pred? (cdr xs)))
  7. (else #f)))
  8.  
  9. (define gender-words '(
  10. ("boy" "girl") ("girl" "boy")
  11. ("boyfriend" "girlfriend") ("girlfriend" "boyfriend")
  12. ("father" "mother") ("mother" "father")
  13. ("husband" "wife") ("wife" "husband")
  14. ("brother" "sister") ("sister" "brother")
  15. ("he" "she") ("she" "he")
  16. ("his" "her") ("her" "his")
  17. ("male" "female") ("female" "male")
  18. ("man" "woman") ("woman" "man")
  19. ("mr" "ms") ("mr" "ms")
  20. ("sir" "madam") ("madam" "sir")
  21. ("son" "daughter") ("daughter" "son")
  22. ("uncle" "aunt") ("aunt" "uncle")))
  23.  
  24. (define (split str)
  25. (let loop ((cs (string->list str)) (word (list)) (words (list)))
  26. (cond ((null? cs)
  27. (reverse (if (null? word) words
  28. (cons (list->string (reverse word)) words))))
  29. ((char-alphabetic? (car cs))
  30. (loop (cdr cs) (cons (car cs) word) words))
  31. ((pair? word)
  32. (loop (cdr cs) (list) (cons (string (car cs))
  33. (cons (list->string (reverse word)) words))))
  34. (else (loop (cdr cs) word (cons (string (car cs)) words))))))
  35.  
  36. (define (replace word)
  37. (let* ((d (string-downcase word))
  38. (w (assoc d gender-words))
  39. (w (if w (cadr w) d))
  40. (w (string->list w)))
  41. (cond ((all? char-upper-case? (string->list word))
  42. (apply string (map char-upcase w)))
  43. ((char-upper-case? (car (string->list word)))
  44. (list->string (cons (char-upcase (car w)) (cdr w))))
  45. (else (list->string w)))))
  46.  
  47. (define (change-gender str)
  48. (apply string-append (map replace (split str))))
  49.  
  50. (display (change-gender "My Brother's girlfriend is taking HER sister to the movies."))
Success #stdin #stdout 0.04s 8808KB
stdin
Standard input is empty
stdout
My Sister's boyfriend is taking HIS brother to the movies.