Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Created April 3, 2022 17:12
Show Gist options
  • Save samdphillips/844cdc3356681ba2ba0fd5aefc77e214 to your computer and use it in GitHub Desktop.
Save samdphillips/844cdc3356681ba2ba0fd5aefc77e214 to your computer and use it in GitHub Desktop.
#lang racket/base
(define (apples a-string)
(define string-size (string-length a-string))
(define (once current count i)
(cond
[(= i string-size)
(if current
(list (cons current count))
null)]
[else
(define ch (string-ref a-string i))
(cond
[(not current) (once ch 1 (add1 i))]
[(char=? ch current) (once current (add1 count) (add1 i))]
[else
(cons (cons current count)
(once ch 1 (add1 i)))])]))
(once #f #f 0))
(apples "aaaabbbcca")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment