A collection of articles, ideas, and rambling from a guy who wrote some software that one time.

Thursday, August 03, 2006

a scheme program

some jerk is not learning about functional programming

(define (parse callback)
(lambda (data)
(if (>= (string-length data) 10)
((parse (callback (substring data 0 10))) (substring data 10))
(lambda (moredata)
((parse callback) (string-append data moredata))))))

(define (make-line-writer n)
(lambda (line)
(display (format "~A: '~A'\n" n line))
(make-line-writer (+ n 1))))

((((((parse (make-line-writer 1))
"hello") " world") " radix") " is") " dumb!!!!!!!!!!!!!!!")

Update: In addition to not being buggy, I've changed it so that this version is actually functional as well.
Update 2: Forgot the formatting change in the last update.

2 comments:

carmstro said...

While you may have written a less buggy version, you show your ignorance of functional programming by claiming that my version was not functional. Given this obvious incompetence on your point, I am forced to call into question the quality of any code you have ever written.

glyf said...

Just in case anyone is trying to learn something from our generally unhelpful blog antics ... :)

Chris's original version had a bug where it wouldn't call your callback with the last length-10 string. It was technically "purely functional" because it didn't create any side effects. My first version fixed the bug.

However, my first version, like his, didn't capture a new callback as the result of the old one, so any actual application using either would have been forced to maintain state in their callback object in order to do anything useful. The version here is actually useful in the context of a purely functional program.

Also I do not actually think that Chris is a jerk, just like Penn did not actually saw off both of Teller's hands.