(defun continued-fraction (real)
(multiple-value-bind (integer-part fractional-part)
(floor (rationalize real))
(cons integer-part (if (zerop fractional-part)
nil
(continued-fraction (/ 1 fractional-part))))))
What does /prog/ think? Am I shooting myself in the foot by just throwing rationalize in there and hoping it works out?
Name:
Anonymous2007-09-04 2:12 ID:Yf1b6cem
>>34
Okay, if you want it. Here's updated code that copes with floating point bullshit by sidestepping it entirely.
(defun continued-fraction (real)
(when (stringp real) (return-from continued-fraction
(continued-fraction (read-as-rational real))))
(when (floatp real) (return-from continued-fraction
(continued-fraction (read-as-rational (write-to-string real)))))
(multiple-value-bind (integer-part fractional-part)
(floor real)
(cons integer-part (unless (zerop fractional-part)
(continued-fraction (/ fractional-part))))))
;;; Found replace-all on http://cl-cookbook.sourceforge.net/strings.html
(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part
is replaced with replacement."
(with-output-to-string (out)
(loop with part-length = (length part)
for old-pos = 0 then (+ pos part-length)
for pos = (search part string
:start2 old-pos
:test test)
do (write-string string out
:start old-pos
:end (or pos (length string)))
when pos do (write-string replacement out)
while pos)))