Skip to content

Instantly share code, notes, and snippets.

@Josh-Tilles
Created September 8, 2011 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Josh-Tilles/1205029 to your computer and use it in GitHub Desktop.
Save Josh-Tilles/1205029 to your computer and use it in GitHub Desktop.
Programming Praxis 19 Aug 2011 (first non-repeating char) solution in PLT Racket
#lang racket
(define (first-uniq chars)
(when (empty? chars)
(raise "There are no unique letters"))
(let ([c (first chars)])
(if (memq c (rest chars)) ; MEMQ is like MEMBER with EQ?
(first-uniq (remq* (list c) chars)) ; N.B the tail-recursion here
c)))
(require rackunit)
(check-equal? (first-uniq (string->list "aabcbcdeef"))
#\d)
(check-equal? (first-uniq (string->list "faabcbcdeef"))
#\d)
(check-equal? (first-uniq (string->list "aabcbcdeefd"))
#\f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment