array literal and Tail Call Optimization

Started by ssqq, May 04, 2014, 06:27:51 AM

Previous topic - Next topic

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:


    >[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

#1
Hi ssqq,



For the second part of your question have a look at the code patterns of newLisp

http://www.newlisp.org/downloads/CodePatterns.html#toc-5">//http://www.newlisp.org/downloads/CodePatterns.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.


; 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


cormullion

#2
Some previous posts:



http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=5&t=4131&hilit=tail+call">//http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=5&t=4131&hilit=tail+call



http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=15&t=2748&hilit=tail+call">//http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=15&t=2748&hilit=tail+call



http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=5&t=4131&hilit=tail+call">//http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=5&t=4131&hilit=tail+call



http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=12&t=4319&hilit=tail+call">//http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=12&t=4319&hilit=tail+call

ssqq

#3
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).