public
Created

Programming Praxis 19 Aug 2011 (first non-repeating char) solution in PLT Racket

  • Download Gist
Praxis19Aug2011.rkt
Racket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.