Tuesday, June 30, 2009

ruby-fying scheme

So, I was reviewing ID3A and realized that there was a potential usage (mostly for myself) to port it into PocketScheme for my ipaq.

Then, I hit upon the brick wall that is string manipulation (or lack thereof) in Scheme.
For example, there is no String.reverse equivalent (hint: convert it to a list, then reverse, then back to a string) so I thought I would begin my porting all of my favourite Ruby commands over to Scheme.

First up: Array.join => list-join


; list-join works the same way that Array.join in ruby works
; in other words, take an array and make a delimited string
; "one,two,three"
; a simple way to build strings for file output
; x-> list
; y-> delimiter (must be a char)
(define (list-join x y)
(define o (open-output-string))
(let iter (( i 0 ))
(if (= i (length x)) (newline)
(begin
(display (list-ref x i) o)
(if (< i (- (length x) 1)) (write-char y o))
(iter (+ i 1)
)
)
)
)
(get-output-string o)
)

No comments:

Post a Comment