Skip to content

Instantly share code, notes, and snippets.

Created Feb 5, 2021
What would you like to do?
#lang racket
(define (f s)
(define ls (string->list s))
(reverse (for/fold ([rle `((,(car ls) . 1))])
([c (cdr ls)])
(match-define (list (cons v n) previous ...) rle)
(if (char=? v c)
(cons (cons v (add1 n)) previous)
(cons (cons c 1) rle)))))
(define (g s)
(let loop ([ls (string->list s)])
(match ls
[(list) empty]
[(list a _ ...)
(define-values (left right) (splitf-at ls (curry equal? a)))
(cons (cons (length left) a)
(loop right))])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment