Skip to content

Instantly share code, notes, and snippets.

@juanibiapina
Created November 25, 2014 02:16
Show Gist options
  • Save juanibiapina/973608f79b5a9f69f0d5 to your computer and use it in GitHub Desktop.
Save juanibiapina/973608f79b5a9f69f0d5 to your computer and use it in GitHub Desktop.
Functional printf
#lang racket
(define (to-function format acc)
(match format
[(list #\% #\d rest ...) (lambda (i) (to-function rest (string-append acc (number->string i))))]
[(list #\% #\s rest ...) (lambda (s) (to-function rest (string-append acc s)))]
[(cons c rest) (to-function rest (string-append acc (string c)))]
[null acc]))
(define (printf format)
(to-function (string->list format) ""))
(((printf "%d hello %s") 1) "some string")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment