🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of cousinitt

cousinitt's solution

to Acronym in the Racket Track

Published at Apr 13 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

For installation and learning resources, refer to the exercism Racket page.

You can run the provided tests through DrRacket, or via the command line.

To run the test through DrRacket, simply open the test file and click the 'Run' button in the upper right.

To run the test from the command line, run the test from the exercise directory with the following command:

raco test acronym-test.rkt

which will display the following:

raco test: (submod "acronym-test.rkt" test)
2 success(es) 0 failure(s) 0 error(s) 2 test(s) run
2 tests passed


Julien Vanier https://github.com/monkbroc

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#lang racket/base

(require "acronym.rkt")

(module+ test
  (require rackunit rackunit/text-ui)

  (define suite
     "acronym tests"

     (test-equal? "basic"
                  (acronym "Portable Network Graphics")

     (test-equal? "lowercase words"
                  (acronym "Ruby on Rails")

     (test-equal? "camelcase"
                  (acronym "HyperText Markup Language")

     (test-equal? "punctuation"
                  (acronym "First In, First Out")

     (test-equal? "all caps words"
                  (acronym "PHP: Hypertext Preprocessor")

     (test-equal? "non-acronym all caps word"
                  (acronym "GNU Image Manipulation Program")

     (test-equal? "hyphenated"
                  (acronym "Complementary metal-oxide semiconductor")

  (run-tests suite))
#lang racket

(provide (contract-out
          [acronym (string? . -> . string?)]))

; Walk through the string a character at a time, peeking at the next character.
; If the head character is lower case and the next character is upper case,
; then put a space between them, and skip past the next (thus, cddr). Otherwise
; cons the head character on to the output list.
(define (space-camel-case s)
  (let ([as-chars (string->list s)])
    (define (space-inserter lst)
      (cond [(< (length lst) 2) lst]
            [(and (char-lower-case? (car lst))
                  (char-upper-case? (cadr lst)))
             (cons (car lst) (cons #\space (cons (cadr lst) (space-inserter (cddr lst)))))]
            [else (cons (car lst) (space-inserter (cdr lst)))]))
    (list->string (space-inserter as-chars))))
(define (acronym s)
  (let ([camel-safe (space-camel-case s)])
     (map (λ(u)
            (string-upcase (substring u 0 1)))
           (string-replace camel-safe "-" " "))) ""))) ; the empty string here is used by join

Community comments

Find this solution interesting? Ask the author a question to learn more.

cousinitt's Reflection

I liked the camel-case twist.