Created
February 5, 2021 21:56
-
-
Save winny-/bbb672534ad74cce005776d64697b647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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