Skip to content

Instantly share code, notes, and snippets.

@Glorp
Last active March 26, 2017 12:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Glorp/e615a9b51dc74d8f93a30434c009d06d to your computer and use it in GitHub Desktop.
Solving a number problem using idiomatic LISP
#lang racket
(define (read-string s)
(with-handlers
([(λ (_) #t) (λ (_) 0)])
(define in (open-input-string s))
(let loop ()
(unless (eof-object? (read in))
(loop)))
1))
(define (number->chars n)
(string->list (~a n
#:min-width 6
#:pad-string "0")))
(define (char->string c)
(match c
[#\0 "((((((("]
[#\1 "(((((("]
[#\2 "((((("]
[#\3 "(((("]
[#\4 "((("]
[#\5 "(("]
[#\6 "("]
[#\8 ")"]
[#\9 "))"]
[_ "horses"]))
(define (number->string n)
(define char-list
(map char->string (number->chars n)))
(list->string
(sort (append* (map string->list char-list))
char<?)))
(for/sum ([n (in-range 1000000)])
(read-string (number->string n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment