• 0 Posts
  • 2 Comments
Joined 11 months ago
cake
Cake day: October 17th, 2023

help-circle
  • What is purpose of this? Optimize a function which uses a terrible algorithm? Why not use a good algorithm? In Racket for instance

    (define (memoize/1 f)
      (define h (make-hasheqv))
      (λ (a)
        (hash-ref! h a (thunk (f a)))))
    
    (define fib
      (memoize/1
       (λ (x)
         (if (< x 2) 
             1
             (+ (fib (- x 2)) 
                (fib (- x 1)))))))
    

    And now

    > (time (fib 39))
    cpu time: 0 real time: 0 gc time: 0
    102334155
    > (time (fib 50000))
    cpu time: 77 real time: 88 gc time: 29
    174387413787277868303885543...
    

    (10,000 digit number, results are from cold Racket so memo table was empty initially.)

    Is entertaining then to try to compute say (log (fib 500000) 10). Bignums tend to get pretty slow then.


  • nil being self-evaluating is not strange. Since nil is a symbol it can be defined as a constant. So there is nothing to stop a Lisp implementation saying (in CL):

    (defconstant nil 'nil)
    

    just the same as

    (defconstant t 't)
    

    What is strange is these things:

    strange thing true in CL? true in elisp? true in Scheme?
    () is a list but not a cons yes yes yes
    () is both a list and a symbol yes yes no
    () is self-evaluating yes yes no