Skip to content

Instantly share code, notes, and snippets.

@winny-
Created February 5, 2021 21:56
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 winny-/bbb672534ad74cce005776d64697b647 to your computer and use it in GitHub Desktop.
Save winny-/bbb672534ad74cce005776d64697b647 to your computer and use it in GitHub Desktop.
#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