Depends, that is the very standard way of doing the iterative factorial function. The other standard method is recursive:
(define (factorial n)
(if (< n 2)
1
(n * (factorial (- n 1)))))
If you're a dick, there's equivalent forms in message passing style, continuation passing style, the Y-recombinator version, and if your maximum autism SKI church numeral style.
primePowerOf :: Integer -> Integer -> Integer
primePowerOf n p = (n - s p n) `div` (p - 1)
where s p 0 = 0
s p n = let (q,r) = n `divMod` p in s p q + r
factorisedFactorial :: Integer -> [(Integer,Integer)]
factorisedFactorial n = parMap rseq (ap (,) $ primePowerOf n) $ takeWhile (<= n) primes
>>12
What are local variables using as memory then faggot?
Name:
Anonymous2012-09-25 17:48
I don't understand why you guys are bothering to discuss this, it's just an implementation detail that has nothing to do with C itself and is completely unrelated to >>2.
>>15
Well in a regular x86 implementation you could use the heap if you wanted to, even for local variables, the compiler could just insert malloc and free where it needed to.
Name:
Anonymous2012-09-25 17:53
I always laughed when sussman claimed doing what OP did was an itetative approach when it's clearly slow ass recursion
>>12
what do you mean he's right? The shit that code gets compiled into uses the stack. is the generated machine code and the x86 asm shit one in the same logically?
Name:
Anonymous2012-09-25 17:57
>>18
And what happens when i have 100 local variables?
>>19
There's a lot more architectures than just shitty x86.
>>20
The compiler says ``Fuck you." except on IA-64, where it just uses 100 of the 128 registers.
Name:
Anonymous2012-09-25 18:06
>>18
Even with an Itanium I don't see how registers alone could be used to create a C implementation, a conforming implementation must be able to handle at least 511 identifiers with block scope declared in one block and be able to handle at least 127 nesting levels of blocks, that's a lot of registers, especially when a single object can consist of 65535 bytes (hosted environment only).
Name:
Anonymous2012-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:
Anonymous2012-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
>>19
I don't think you understand what's going on here. That's what your compiler does. That's what my compiler does. It is not what every compiler in the set of all conforming C compilers do.
>>26
Are you saying some C compilers don't use the stack, or that its just not necessary? Where the fuck does it keep the thread environment? I suppose C itself isn't really reliant on the stack, which I suppose would explain why you can't access the stack from C.
>>28
If you think that's arcane magic, just wait til you see the rest!
Name:
Anonymous2012-09-25 19:38
>>29
lel
The best part of scheme is when you can use an infinite list of accelerators to accelerate the convergence of the previous element, where the first element is an infinite list of a converging series.
>>28 Are you saying some C compilers don't use the stack, or that its just not necessary?
As long as we're not talking of extant ones, sure. Otherwise that's not what I'm saying.
Where the fuck does it keep the thread environment?
Wherever it wants. It's implementation defined. You've already been given an example. Think up your own!
I suppose C itself isn't really reliant on the stack
Holy fuck. How many times have you been told this very thing just now?
>>37
Prior to that you were told 5 times that it isn't part of the spec, 3 of those times it was explained that it isn't necessary for implementation (because *it's not part of the spec*)
Name:
Anonymous2012-09-25 22:33
>>38
I have discovered a truly marvelous proof of it, which this post is too small to contain.