Created
March 25, 2016 02:47
-
-
Save Carpetfizz/206226ba373bb41c677e to your computer and use it in GitHub Desktop.
A scheme implementation of the infamous leftpad.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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