array literal and Tail Call Optimization

For the Compleat Fan
Locked
ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

array literal and Tail Call Optimization

Post 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?

fdb
Posts: 66
Joined: Sat Nov 09, 2013 8:49 pm

Re: array literal and Tail Call Optimization

Post 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



ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: array literal and Tail Call Optimization

Post 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).

Locked