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
I have managed to break the boundaries of posts and physically connect the content of them. Am I an EXPERT BBCODE PROGRAMMER?̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅
I remember some guy giving lectures at conferences about using Lisp to imitate other languages like Perl. So you could type Perl-like code and have this Lisp program understand it. Do any of you know about that?
Name:
Anonymous2007-09-04 10:21 ID:1lDhSzch
>>49
It can supposedly be done with Lisp macros, but I have so far not seen a single Lisp programmer than knows how to use macros beyond for-loops and other stupid stuff.
Name:
Anonymous2007-09-04 11:26 ID:eOpLdWqF
>>49
It's fairly simple in concept. Since macros don't evaluate their parameters, you can make a form like (perl $foo=3 ... ) expand to whatever, by parsing the symbols given as parameters to the perl macro (defmacro perl (&whole code) ... ).
Of course, the tricky bit is that such a macro definition will just give you a bunch of symbols like the above "$foo=3". You can make the situation a little bit better by stipulating spaces wherever possible ("$foo = 3"), but you're still stuck doing pattern matching on symbols for variable names (owing to sigils and such), which means you'll probably be treating them as strings and losing the ability to do simple backquote substitution of symbol values into literal text forms:
(defmacro mact (name val)
`(let ((,name ,val))
(print ,name)))
(macroexpand-1 '(mact foo 3))
(LET ((FOO 3))
(PRINT FOO))
Basically, parsing something like perl code with a macro is pretty much akin to parsing it as plain text. Macros are great if you're implementing a language with no semantic content in its symbols, meaning that any given symbol either has a known meaning or is a new name for something, but when a given symbol may be a name and a programmatic instruction, it gets tricky fast. Take a look at the source for your favorite Lisp's loop macro and you'll see that, even when the language to be parsed is simple, constructing working code out of it is pretty hairy (1k - 2k LOC hairy).
On the plus side, if I were writing a parser for something Lisp would be more fun than C or something to write it in.