Dear Scheme wizards, I have a confession to make: I can never remember how to write loops in Scheme using the named-let convention. I’m working on a problem from the British Informatics Olympiad which you can read about here, with my own ugly imperative Ruby solution here. (My apologies to real Ruby programmers, of course.)
I’ll no doubt be apologizing again after I share my Scheme solution. Until then, here is some code to read in the contents from a file. This works in MIT Scheme but should be portable to any R5RS Schemes.
read will blow up if the file contains certain characters, like
#\#. MIT Scheme provides additional procedures like
read-line to solve this problem.
(with-input-from-file "/home/rml/Desktop/current/INPUT.TXT" (lambda () (let loop ((source (read)) (sink '())) (if (eof-object? source) (reverse sink) (loop (read) (cons source sink)))))) ;Value 36: (5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5)