[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/dL1Uy4sR    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Feb 22:
; remove characters from a string

(define (string-index c str)
  (let loop ((ss (string->list str)) (k 0))
    (cond ((null? ss) #f)
          ((char=? (car ss) c) k)
          (else (loop (cdr ss) (+ k 1))))))

(define (filter pred? xs)
  (let loop ((xs xs) (ys '()))
    (cond ((null? xs) (reverse ys))
          ((pred? (car xs))
            (loop (cdr xs) (cons (car xs) ys)))
          (else (loop (cdr xs) ys)))))

(define (complement f) (lambda xs (not (apply f xs))))

(define (right-section proc . args)
  (lambda xs (apply proc (reverse
    (append (reverse args) (reverse xs))))))

; lisp-style recursion down the cdrs of a list
(define (string-remove str match)
  (let loop ((ss (string->list str)) (zs (list)))
    (cond ((null? ss) (list->string (reverse zs)))
          ((string-index (car ss) match) (loop (cdr ss) zs))
          (else (loop (cdr ss) (cons (car ss) zs))))))

(display (string-remove "Programming Praxis" "aeiou")) (newline)

; c-style walk the string using pointers
(define (string-remove str match)
  (set! str (apply string (string->list str)))
  (let loop ((src 0) (dest 0))
    (cond ((= (string-length str) src)
            (substring str 0 dest))
          ((string-index (string-ref str src) match)
            (loop (+ src 1) dest))
          (else (string-set! str dest (string-ref str src))
                (loop (+ src 1) (+ dest 1))))))

(display (string-remove "Programming Praxis" "aeiou")) (newline)

; haskell-style using combinators
(define (string-remove str match)
  (list->string
    (filter
      (complement (right-section string-index match))
      (string->list str))))

(display (string-remove "Programming Praxis" "aeiou")) (newline)


Output:
1
2
3
Prgrmmng Prxs
Prgrmmng Prxs
Prgrmmng Prxs


Create a new paste based on this one


Comments: