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

Racket

Name: Anonymous 2012-09-25 13:28

there must be a better way to do this right


(define (factorial n)
    (define (iter n result)
        (if (= n 0)
            result
            (iter (- n 1) (* result n))))
    (iter n 1))

Name: Anonymous 2012-09-25 18:10

>>5
Now that we are talking about implementation details I would like to bring notice that the function would not produce a stack overflow when compiled with GCC and optimization on x86, the call instruction is nowhere to be found in the resulting object code it only uses jmp.

Name: Anonymous 2012-09-25 18:12

>>24
Here is the disassembly of the object file from my machine.

00000000 <factorial>:
   0:   8b 54 24 04             mov    edx,DWORD PTR [esp+0x4]
   4:   b8 01 00 00 00          mov    eax,0x1
   9:   85 d2                   test   edx,edx
   b:   74 0b                   je     18 <factorial+0x18>
   d:   8d 76 00                lea    esi,[esi+0x0]
  10:   0f af c2                imul   eax,edx
  13:   83 ea 01                sub    edx,0x1
  16:   75 f8                   jne    10 <factorial+0x10>
  18:   f3 c3                   repz ret

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