PasteRack.org
Paste # 87760
2014-11-23 10:15:55

Fork as a new paste.

Paste viewed 323 times.


Embed:

  1. #lang racket
  2.  
  3. ; Convert a string into a list of string pairs
  4. ; ex: (string->pairs "Apple") ("Ap" "pl" "e")
  5. (define (string->pairs s)
  6.     (define (loop p l)
  7.       (cond
  8.         [(empty? l) (reverse p)]
  9.         [(eq? (length l) 1) (reverse (cons (list->string l) p))]
  10.         [else (loop (cons (list->string (take l 2)) p) (drop l 2))]))
  11.     (loop '() (string->list s)))
  12.  
  13. (print (string->pairs "Apple"))
  14. (print (string->pairs "Supercalifragalisticexpealidocious"))

=>

'("Ap" "pl" "e")

'("Su" "pe" "rc" "al" "if" "ra" "ga" "li" "st" "ic" "ex" "pe" "al" "id" "oc" "io" "us")