PasteRack.org
Paste # 49647
2014-11-23 10:14:54

Fork as a new paste.

Paste viewed 223 times.


Embed:

string into string pairs

  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"))

=>

string-pairs: undefined;

 cannot reference an identifier before its definition

  in module: 'm

string-pairs: undefined;

 cannot reference an identifier before its definition

  in module: 'm