Skip to content

Instantly share code, notes, and snippets.

@Carpetfizz
Created March 25, 2016 02:47
Show Gist options
  • Save Carpetfizz/206226ba373bb41c677e to your computer and use it in GitHub Desktop.
Save Carpetfizz/206226ba373bb41c677e to your computer and use it in GitHub Desktop.
A scheme implementation of the infamous leftpad.js
(define (leftpad s l c)
(if (number? s)
(set! s (number->string s))
)
(if (number? c)
(set! c (number->string c))
)
(define (_leftpad _s _l _c)
(if (<= _l 0)
_s
(_leftpad (string-append _c _s) (- _l 1) _c)
)
)
(_leftpad s (- l (string-length s)) c)
)
; https://repl.it/BzZZ/2
; (leftpad "foo" 5 " ")
; => " foo"
; (leftpad "foobar" 6 " ")
; => "foobar"
; (leftpad 1 2 0)
; => "01"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment