Page 1 of 1

array literal and Tail Call Optimization

Posted: Sun May 04, 2014 1:27 pm
by ssqq
Hello everyone,

I found it is same that array literal with list literal, but they are different datatype.

(list? (array 3)) => nil
(array 3) => (nil nil nil)
'(nil nil nil) => (nil nil nil)
(list? '(nil nil nil) => true

other question, When I run:

Code: Select all

>[cmd](define (fibonacci n)
    (if (< n 2)
    1
    (+ (fibonacci (- n 1))
    (fibonacci (- n 2)))))
[/cmd]
>(fibonacci 100)
My comuter could not print result in ten minutes.

I like recursion, newLISP Hav not Tail Call Optimization?

Re: array literal and Tail Call Optimization

Posted: Sun May 04, 2014 5:04 pm
by fdb
Hi ssqq,

For the second part of your question have a look at the code patterns of newLisp
http://www.newlisp.org/downloads/CodePa ... html#toc-5 how you can speed up things with iteration or memorisation, a memoised fibonacci on my laptop takes 0.014 microseconds with an argument of 100.

Code: Select all

; speed up a recursive function using memoization
(define-macro (memoize mem-func func)
    (set (sym mem-func mem-func)
        (letex (f func  c mem-func)
          (lambda ()
              (or (context c (string (args)))
              (context c (string (args)) (apply f (args))))))))

(memoize fibo
    (lambda (n)
        (if(< n 2) 1
            (+  (fibo (- n 1))
                (fibo (- n 2))))))

> (fibo 100)
1298777728820984005
> (time (fibo 100))
0.018


Re: array literal and Tail Call Optimization

Posted: Sun May 04, 2014 5:46 pm
by cormullion

Re: array literal and Tail Call Optimization

Posted: Mon May 05, 2014 1:39 am
by ssqq
Thanks above nice reply.

I understand that Tail Call Optimization and literal of (Array 1 2 3) or (Hash 'a 1 'c 2) could slove by programmer-self use the framework of newLISP(macro or context).