Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Continued Fractions

Name: Anonymous 2007-09-03 14:57 ID:Yg9/l8CK

(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: Anonymous 2007-09-03 15:15 ID:Heaven

>>1
One word, the incomprehensible indentation of code.  Thread over.

Name: Anonymous 2007-09-03 15:21 ID:vv7fue/E

))))))

Name: Anonymous 2007-09-03 15:23 ID:UWxL6kva

What's wrong about parens? Aren't they better that forced indentation of the code?

Name: Anonymous 2007-09-03 15:23 ID:Yg9/l8CK

>>2
One word: whatever do you mean?

Name: Anonymous 2007-09-03 15:28 ID:Heaven

(defun fraction-continued (partials)
  (if (null partials) 1
    (+ (first partials) (/ (fraction-continued (rest partials))))))

Name: Anonymous 2007-09-03 15:29 ID:1WCt9T06

>>6
USE ENDP INSTEAD OF NULL YOU FUCKING IDIOT!!!!

Name: Anonymous 2007-09-03 15:31 ID:Yg9/l8CK

>>6
Fuck floating point numbers.

Name: Anonymous 2007-09-03 15:33 ID:1WCt9T06

>>8
Learn lisp you idiot lol
integer / integer -> rational

Name: Anonymous 2007-09-03 15:41 ID:Yg9/l8CK

>>9
Yeah, but to get results from a floating point input you've got to convert it to a rational... and if you divide out the fraction returned by that second function (written by someone other than OP, BTW), you'll get something other than the input that would make continued-fraction yield the continued fraction given to the second function.

Name: Anonymous 2007-09-03 15:46 ID:Heaven

>>1

I > U

(defun ensure-continued-fraction (real-number &aux (number (rationalize real-number)))
  (
multiple-value-bind (coefficient remainder) (floor number)
    (
cons coefficient (when (> remainder 0)
                        (
ensure-continued-fraction (/ remainder))))))

Name: Anonymous 2007-09-03 15:48 ID:1WCt9T06

>>10
jabber jabber jabber jabber jabber jabber jabber jabber jabber jabber jabber jabber jabber jabber jabber

This shows that you don't know what you're talking about

Name: Anonymous 2007-09-03 15:52 ID:YjKdlAZ6

>>11
You don't understand indentation.

(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))))))

Fixed (I hope).

Name: Anonymous 2007-09-03 15:52 ID:Heaven

Oops!  Anyway, Lisp is gay.

Name: Anonymous 2007-09-03 15:54 ID:1WCt9T06

>>13
HAHAHAHAHA
that code is horrible
You fucking failed it
L-O-L-O-L-O-L-O-L-O-L

Name: Anonymous 2007-09-03 15:57 ID:Yg9/l8CK

>>11
That looks pretty familiar.

Name: Anonymous 2007-09-03 15:59 ID:Heaven

>>16
Yes, every recursive function in lisp looks the same. I just wrote this this second to show OP how shitty his style is though.

Name: Anonymous 2007-09-03 16:01 ID:1WCt9T06

PROTIPS:
 * (/ 1 x) ≡ (/ x)
 * &aux 4lyfe
 * name things PROPERLY, as if you aren't a FAGGOT
 * (if x nil y) FAGGOTRY! use (when x y) instead
 * ZEROP?? are you fucking [b]GAY[/b] or somthing?
 * there is so fucking much wrong with your 5 lines of shit code OP, I desipair

Name: Anonymous 2007-09-03 16:01 ID:Yg9/l8CK

>>17
I especially like the way you replaced every meaningful name in the function with something worse. That was a real brainstorm.

Name: Anonymous 2007-09-03 16:03 ID:1WCt9T06

>>19
learn english, his code is far better than yours

Name: Anonymous 2007-09-03 16:07 ID:Yg9/l8CK

He has me on / and if instead of unless, but fails it completely on all other counts.
* &aux -- why?
* ensure? coefficient does not mean that.
* zerop 4lyfe

Name: Anonymous 2007-09-03 16:07 ID:Heaven

>>20
lol thanks

Name: Anonymous 2007-09-03 16:09 ID:APWoU1Gx

>>20
True.

Name: Anonymous 2007-09-03 16:10 ID:1WCt9T06

1)

(defun foo (a b c &aux (sum (+ a b c)))
 ...)


is like

(defun foo (a b c)
 (let* ((sum (+ a b c)))
  ...))


most lispers consider it bad style

2)
ensure is a CL naming convention, without years of experience (with other CL libraries) you wouldn't understand it.

3)
zerop is fail, sorry but it fucking is
where is my
ONEP TWOP THREEP etc?

Name: Anonymous 2007-09-03 16:19 ID:Yg9/l8CK

>>24
3)
WRITE THEM YOURSELF

CL IS EXTENSIBLE

I love zerop.

Name: Anonymous 2007-09-03 16:24 ID:Yg9/l8CK

>>24
As tempting as using &aux to look cool is, it's not like I added an extra let form or anything. I just threw in a rationalize where it needed to be rather than creating extra variable names and doing more typing.

Name: Anonymous 2007-09-03 16:26 ID:Yg9/l8CK

Lol, from http://www.lisp.org/table/style.htm:
# Don't use the &AUX lambda-list keyword. It is always clearer to define local variables using LET or LET*.

ITT we shoot our mouths off and get caught.

Name: Anonymous 2007-09-03 16:45 ID:1WCt9T06

>>27
App-ppp-pp-eal to authority!!!
Always wins every argument

lisp.org is a costly domain, so anything written there MUST be right!

Name: Anonymous 2007-09-03 16:48 ID:Yg9/l8CK

>>28
4chan.org can be posted on by anybody, so anything written there MUST be right!

Face it, you don't know shit about Lisp (&aux, zerop), English (coefficient, WTF), or LIFE.

Name: Anonymous 2007-09-03 16:57 ID:1WCt9T06

>>29
YHBT LOL

Name: Anonymous 2007-09-03 16:58 ID:Yg9/l8CK

>>30
SERIOUS BISNASS

Name: Anonymous 2007-09-03 17:38 ID:1WCt9T06

>>31
^_^

Name: Anonymous 2007-09-03 20:16 ID:1WCt9T06

LOL

Name: Anonymous 2007-09-04 1:29 ID:BMRJi5rV

MOAR!!!!!!!!

Name: Anonymous 2007-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))))))

(defun read-as-rational (string)
  (let* ((read-value (read-from-string string)))
    (cond ((not (numberp read-value)) (error "Attempted to treat input string as number: failed."))
      ((rationalp read-value) (return-from read-as-rational read-value)) ;; No foul.
      ((floatp read-value)
       (let* ((bare-number (replace-all string "d0" ""))
          (clean-string (replace-all bare-number "." ""))
          (number-length (length clean-string))
          (radix-pos (- number-length
                (position #\. bare-number :from-end nil)))
          (divisor (expt 10  radix-pos)))
         (/ (read-from-string clean-string)
        divisor))))))
 
;;; 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)))

Name: Anonymous 2007-09-04 2:27 ID:+mjGaGsI

>>29
Give it up, kid. You don't know SHIT about JAPAN.

Name: Anonymous 2007-09-04 4:06 ID:BMRJi5rV

>>35
gross dude, Fuck you and fuck your code
Get the fuck off /prog/
this is the most horrible shit ever, no wonder people dont like lisp with fucking noobs like you write code this vile

>>36
LOL!!!!!!

Name: Anonymous 2007-09-04 14:27 ID:BMRJi5rV

yuck!

Name: Anonymous 2009-03-06 10:08


Wokon to fix up  also because of  of the non  exceptionally scary tower.

Name: Anonymous 2011-02-04 13:01

Don't change these.
Name: Email:
Entire Thread Thread List