i'd love to see what this would look like in lisp... function(n){
return (function(i){
return i==1?0:arguments.callee(i-1)+(function(i){
return i==1?1:arguments.callee.caller(i-1)
})(i-1)
})(n+1)
}
Name:
Anonymous2007-08-31 23:30 ID:itA3f2Zb
>>7
I'd rewrite it in Lisp if I had some idea what it did.
baby 1 = 1
baby (i+1) = adult i
adult 1 = 0
adult (i+1) = adult i + baby i
fib n = adult (n + 1)
Name:
Anonymous2007-09-01 0:17 ID:div4kVll
>>9 >>10
Oh, I see what that is now. It's definitely possible in Lisp, but it's going to involve six lambdas or so. I don't have it sorted out in my mind yet.
>>2
this is because people who do not use lisp etc. do not think at all when they program
Name:
Anonymous2007-09-01 1:00 ID:div4kVll
Well, there you go. :/ sort of. The functions aren't created within one another (that would be more work), and using lambda forms to define temporary names feels like cheating.
(funcall ((lambda (c a) #'(lambda (n) (funcall a c a (1+ n))))
#'(lambda (c a i)
(if (= i 1)
1
(funcall a c a (1- i))))
#'(lambda (c a i)
(if (= i 1)
0
(+ (funcall a c a (1- i)) (funcall c c a (1- i))))))
i)
Name:
Anonymous2007-09-01 1:01 ID:div4kVll
>>14
That i at the end is where you say you want the ith Fibonacci number.
Name:
Anonymous2007-09-01 1:10 ID:khcLzIi6
>>14
wow, that's ugly.
i bet it'd be even uglier with the functions created within each other.
is there a way to do it without defining temeporary names?
Name:
Anonymous2007-09-01 1:24 ID:LvwPmJgC
CL-USER> (defun fib (n)
(labels ((aux (x y n)
(if (= n 0) y
(aux y (+ x y) (- n 1)))))
(aux 0 1 n)))
FIB
CL-USER> (fib 6)
13
CL-USER> (lambda (recur x y n)
(if (= n 0) y
(funcall recur y (+ x y) (- n 1))))
#<FUNCTION (LAMBDA (RECUR X Y ...)) {11D62D25}>
CL-USER> (lambda (x a b c) (funcall x x a b c))
#<FUNCTION (LAMBDA (X A B ...)) {11D9324D}>
CL-USER> ((lambda (x a b c) (funcall x x a b c))
(lambda (recur x y n)
(if (= n 0) y
(funcall recur recur y (+ x y) (- n 1))))
0 1
6)
13
>>16
Not that I can think of. It looks like whatever language >>7 is in lets you peek at the call stack and re-call functions from it, but that's really just another way of saying a name has been defined. You have to define names somewhere in order to be able to use those names later. Automating and hiding the actual definition in that way is pretty clever. CL doesn't provide a way to get at this info, as far as I know. The arguments.callee would be easy enough to hack into the lambda macro, but arguments.callee.caller (and more levels up?) would be hard.
Name:
Anonymous2007-09-01 1:42 ID:div4kVll
>>17
Labels is even more cheating. Notice that you did not do anything mutually recursive either.
Name:
Anonymous2007-09-01 1:57 ID:LvwPmJgC
>>19
You're a fucking idiot lol
It's trivial to extend the Y combinator (even though we don't even need or want it atm) to deal with mutually recursive functions.
Try understanding the Y combinator and you will see how fucking obvious it is.
While the Y combinator is a perfect fit for making mutually recursive functions it's not what we're FUCKING TALKING ABOUT. It's just a way to pass a function's name to itself. Read >>7, realize that access to the call stack is the issue at hand.
>>21 While the Y combinator is a perfect fit for making mutually recursive functions it's not what we're FUCKING TALKING ABOUT. It's just a way to pass a function's name to itself. Read >>7, realize that access to the call stack is the issue at hand.
You clearly need more background than you half arsed school computing course to understand this discussion. I strongly suggest you read SICP and GTFO.
>>23
I'm not some loser who studies programming in school. You clearly need more background in this thread to understand this discussion. And that's pretty sad, since it's only 25 posts long. Quite frankly, I don't see how it's possible that you haven't caught on by this point. I think it's safe to say that while you may have read all the words in SICP, most of them were lost on you.
>>18 >>7 is javascript.
here's how i'd do it in factor: [ [ dup 1 = [ drop 0 ] [ [ 2dup ] swap slip 1- pick keep -rot slip + ] if ]
[ dup 1 = [ 2drop 1 ] [ 1- swap call ] if ]
rot 1+ pick call 2nip ]
i'd be surprised if you can't do it similarly in lisp.
i know there is a way to access the call stack in factor, but it's not nearly as easy as in javascript, and would probably be even uglier than all the stack shuffling in my version.
Name:
Anonymous2007-09-01 10:11 ID:jsGz2PE8
breaking lexical scope makes for incredibly slow code