fork(2) download
  1. ; camel case
  2.  
  3. (define (camel-case->underscore str)
  4. (let loop ((cs (string->list str)) (zs (list)))
  5. (if (null? cs) (list->string (reverse zs))
  6. (if (char-upper-case? (car cs))
  7. (loop (cdr cs) (cons (char-downcase (car cs)) (cons #\_ zs)))
  8. (loop (cdr cs) (cons (car cs) zs))))))
  9.  
  10. (define (underscore->camel-case str)
  11. (let loop ((cs (string->list str)) (zs (list)))
  12. (if (null? cs) (list->string (reverse zs))
  13. (if (char=? (car cs) #\_)
  14. (if (null? (cdr cs)) (list->string (reverse zs))
  15. (loop (cddr cs) (cons (char-upcase (cadr cs)) zs)))
  16. (loop (cdr cs) (cons (car cs) zs))))))
  17.  
  18. (display (camel-case->underscore "camelCaseToUnderscore")) (newline)
  19. (display (underscore->camel-case "underscore_to_camel_case")) (newline)
Success #stdin #stdout 0.01s 42848KB
stdin
Standard input is empty
stdout
camel_case_to_underscore
underscoreToCamelCase