Skip to content

Instantly share code, notes, and snippets.

@97jaz
Last active October 16, 2016 04:37
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 97jaz/add792e0bc4a24d18f8438bc97cb3f1e to your computer and use it in GitHub Desktop.
Save 97jaz/add792e0bc4a24d18f8438bc97cb3f1e to your computer and use it in GitHub Desktop.
#lang racket/base
;; others: generates a list of characters in `str` *other*
;; than the one at index `pos`
(define (others str pos [i 0] [res '()])
(cond [(= i (string-length str))
(reverse res)]
[(= pos i)
(others str pos (add1 i) res)]
[else
(others str pos (add1 i) (cons (string-ref str i) res))]))
;; each+others: generates a list of lists of characters. Each
;; list's first element is a unique character of `str` and the
;; rest of the elements are the rest of the string's characters.
(define (each+others str [i 0] [res '()])
(cond [(= i (string-length str))
(reverse res)]
[else (each+others str
(add1 i)
(cons (cons (string-ref str i) (others str i))
res))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment