Skip to content
This repository was archived by the owner on Feb 11, 2018. It is now read-only.

Add word-count exercise #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add word-count exercise #2

wants to merge 1 commit into from

Conversation

mbertheau
Copy link
Owner

No description provided.

@lexi-lambda
Copy link

Here's my implementation of this problem, which I think is more Racket-y.

(define (word-count word)
  (for/fold ([result (hash)])
            ([word (in-list (string-split word))])
    (hash-set result word (add1 (hash-ref result word 0)))))

This code uses immutable hash tables. Mutation is usually considered unidiomatic in Racket, so hash-set returns a new hash table instead of updating the existing one. It also uses add1 instead of (+ 1 ...), passes 0 as the default value to hash-ref, and uses for/fold for the accumulation of result.

@mbertheau
Copy link
Owner Author

Nice. Feels much more idiomatic! :)

@mbertheau mbertheau force-pushed the word-count branch 3 times, most recently from a52659b to 08a2941 Compare May 22, 2015 21:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants