Author Topic: Lisp in Basic  (Read 208202 times)

0 Members and 2 Guests are viewing this topic.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #810 on: September 19, 2014, 03:56:37 AM »
Rob,

I do not believe these zero timings belong to true recursion. As long as your code
(f b (+ a b) (- x 1))
is defined properly tail recursive, any contemporary LISP will optimize it transparently to iteration in both interpretative and precompiled environments.

Don't get mislead by the timings. What you see doesn't pertain to recursion. It pertains to the clever implementation of the language that guarantees that each and every occurence of proper tail recursion irrespective of its complexity will be unrolled into equivalent iteration automatically. All you're supposed to do is just emit properly tail recursive code. :D


Oh, and by the way: (the result tends to #INF at larger args due to 64 bit double restrictions)

.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #811 on: September 19, 2014, 04:29:49 AM »
Hi Mike,

Sure, i'm fully aware of the fact Racket Scheme does TCO , but it's the way of writing things ...  in the iterative way, I have to declare both accumulators a & b : resulting in something as (NL style)

(define fibo (lambda (n)
   (let ((a 1) (b 1) (res 0) )                      ;; temporarily res ;;  psetq  (parallel setq in CL sometimes does weird things)
     (dotimes (i n)
         (setq res (+ a b) a b b res))           ;; NL short for ( set! res (+ a b)) (set! a b) (set! b res)  -- needs 3 lines in CL / Scheme
      a )))

You're of course 100% correct , both codes (it & recur) give the same benchmark

It is not so that TCO is completely solved in all CL's -- 

CLisp p.e. uses the GNU Lightning JPTAIL (the difference between self-calls and general TCO)  ---   safe are Clozure CL and Steel Bank CL  (from those I worked with ).
(this is a nice document : http://0branch.com/notes/tco-cl.html )  -- with disassembled code ....

So, my only reason is that a mathematical formula is written shorten in a recursive way ( this doesn't mean I like recursive program flow --  unless binary trees are set up ---      but here NewLisp even has (dotree       )  it is very poor on recursion and doesn't do any optimizing at all ..  and also giving the fact I do not like a helper function is floating around the code somewhere , I like them inside the definition which is possible with Scheme's (letrec ) , CL's (labels ) and NL's (let   )

(the true speed comes from using accumulators , which take all numeric data except the loop index )

best Rob   


Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #812 on: September 19, 2014, 04:48:25 AM »
Hehe moreover, rumor has it there are some LISP gurus (such that are making competitor companies tremble, as you told us once) who are able to write properly tail recursive functions with additional recursive calls placed in such a clever way within the function body that, upon the initial optimization of proper tail recursion occurences, these additional recursions are also promoted to proper tail positions within the unrolled iterations where they can be TCO-ed by the compiler again. :D

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #813 on: September 19, 2014, 12:10:48 PM »
héhé ,   8)

but , be very careful with Lispers -- they do their very best to get bigger fees with less labour ( like that guy writing the macro's in that piece of literature )..

best, Rob


.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic = VICTORY! :D
« Reply #814 on: September 20, 2014, 01:47:16 PM »
Dear friends,

I hasten to inform you that I've been able to fix the OxyLISP garbage collector and now the program works as expected barring a couple minor glitches that I hope to fix shortly.

I think we should all thank Charles for his wonderful work on OxygenBasic. I understand that it's still a W.I.P. in many respects but its capabilities even as they are now have already promoted it to the first ranks of indie BASIC compilers available on the contemporary market.

My toughest test (Rob's ASCII Mandelbrot script) shows that OxyLISP does its interpretative job about four times faster than tinyscheme. You can see the comparative results in the accompanying snapshot with your own eyes. Just FYI, tinyscheme's own typical startup/shutdown time is around 32 milliseconds therefore it hardly affects the overall results as seen in the snapshot.

Bravo Charles! :D

.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #815 on: September 21, 2014, 03:03:43 AM »
Great !!!

Been thinking about your previous post --    there may be very clever ways in Lisp to handle mutual , nested or whatever recursion.  After all Lisp is famous for trampolining , which in fact means that there is a kind of "mother function" which in an iterative way outputs not values but functions -- wrote something ,   the functions generated are not evaluated till a certain condition is reached ,   it are just patterns , here an index has been used , but of course a more general   (while    ) can be used too ,   or a   (catch      (throw  ) construction or whatever ........

;;------------------------------------------------------------------------------

>
(define fac
(lambda (x)
 (let ((pattern (list x '*))
         (print-it-nice (lambda (m)
               (let ((fi (cons 'fac (first m))))
                 (reverse (cons fi (rest m)))))))
    (dotimes (i (- x 1))
   (push (- (first pattern) 1) pattern)
   (println (print-it-nice pattern)))
   (eval (reverse (rest pattern))))))


> (fac 10)
(* 10 (fac 9))
(* 10 9 (fac 8))
(* 10 9 8 (fac 7))
(* 10 9 8 7 (fac 6))
(* 10 9 8 7 6 (fac 5))
(* 10 9 8 7 6 5 (fac 4))
(* 10 9 8 7 6 5 4 (fac 3))
(* 10 9 8 7 6 5 4 3 (fac 2))
(* 10 9 8 7 6 5 4 3 2 (fac 1))
3628800

 8)

Important : only the last list is evaluated , nothing is calculated in-between (in fact , it can't be computed ) !!!! , nothing is stacked somewhere   -- in memory only the length of the list you see is needed.....

(infact I'm using a dirty trick -- the lists I use are reversed during computing, with the operator coming at the tail -- avoiding evaluating    ;) 

best Rob

(only an example of a function generating functions -- for x!   the more compact way is :

(define fac (lambda  (x)  (apply * (sequence 1 x))))

« Last Edit: September 21, 2014, 03:35:26 AM by RobbeK »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #816 on: September 21, 2014, 05:05:35 AM »
Hi Rob,

Thanks for the example. I understand your theory behind your code (LISP code being ordinary datum if quoted, similar to data in its proper sense) and I can read and understand almost all of it except for (push). What does it actually do?

Now Rob, I need your help.

I)

1. Can Scheme's (do) as per its syntax rules defined in the .CHM that I posted earlier be written as a user-defined function using Lisp-in-Basic's vocabulary as per the .PDF that you also have?

2. Can Scheme's named (let) as defined per the .CHM be written in Lisp-in-Basic's vocabulary?

3. Can Scheme's (letrec) as defined per the .CHM be written in Lisp-in-Basic's vocabulary?

Please note that Lisp-in-Basic permits explicit (define)s local to a procedure and visible only within its own environment.

I need them very very badly. (and I'm currently interested only in OxyLISP (= Lisp-in-Basic) compatible code)


[UPD] Rob, I've been able to do it myself following the directions for the derived expressions' semantics that I found in the R4RS standard. :)


II)

Are there any standard keywords or macros in any of the LISPs known to you that would allow the programmer to selectively suppress REPL's output to the console in some arbitrary parts of their code? Something of the (mute)/(unmute) kind?


Thanks!


P.S. Your (define fac (lambda  (x)  (apply * (sequence 1 x)))) is inoperative in any Scheme that I have including OxyLISP, even if deprecated (sequence) is changed to modern (begin). It yields unacceptable arguments for the (*) procedure.

P.P.S. The 2nd argument to (apply) must be a list, not a sequence. :P
« Last Edit: September 21, 2014, 08:09:56 AM by Mike Lobanovsky »

JRS

  • Guest
Re: Lisp in Basic
« Reply #817 on: September 21, 2014, 10:15:22 AM »
Quote from: Mike
I need them very very badly. (and I'm currently interested only in OxyLISP (= Lisp-in-Basic) compatible code)

Please define what Lisp-in-Basic compatible code means. Does Arthur's implementation meet any of the Scheme standards or is it a hybrid?  Do you have any documentation (other than the original provided by Arthur) that we can use with OxyLISP when you release it?

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #818 on: September 21, 2014, 11:22:03 AM »
Hi John, Mike

-- the sequence is NewLISP nomenclature (oops, sorry) - in Racket Scheme it is (range from to) or the same as a stream (in-range from to)  -- it does not exist in CL either --  here's "a" definition :

(define seq (lambda (from to)
              (letrec (( seq* (lambda (from to L)
                             (if (> from to) (reverse L)
                                 (seq* (+ from 1) to (cons from L))))))
                (seq* from to '()))))

------------------------------ repl

Language: racket; memory limit: 512 MB.
> (seq 1 22)
'(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)
>

--------------------------------

(push  ) is the destructive form of (cons  )  -- (cons ) is constructive , it does not change any of the arguments

(define a (list 1 2))
(push 6 a)
a -> (6 1 2)

however, when at first sight , this looks extremely childish to put it into a definition, it isn't -- it surely isn't ...

(define (push item L)
  (set! L (cons item L)))    does ******NOT******** work  (in any LISP dialect)

the reason is that if L is a list it is represented by the list and not by its location (pointer)
infact this definition does it all correct, but it is constructing a list that is lost and does not reach the location L is pointing at.

the correct definition is : (and it needs a macro)

NewLISP

(define-macro (push it L)
  (setq (eval L) (cons (eval it)  (eval L) )))

& Common Lisp

(defmacro push (x L)
   `(setq ,L (cons ,x ,L)))

(I'll study the Scheme macro's soon -- I will be able to define (push ) and (pop ) then )

----------

(mute) (unmute) 

I only know (silent                   ) in NewLISP ,,   it's a problem too for me in LispIDE    (the Unix/Linux users , most of the time are in EMACS -- which is run in ELisp (so it runs two Lisps at the time -- one is named "inferior Lisp" -- and it should be easy to tell the one Lisp not to emit the output from the other ...   some time ago , there was someone here who worked with Emacs (he introduced himself as something as   "  a not so good programmer " -- he will know (and he's probably a lot better than he thinks)
 

best Rob,

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #819 on: September 21, 2014, 11:26:10 AM »
First of all "Hi John",

We haven't yet met with you today. :)

Lisp-in-Basic is an incomplete and inconsistent subset of R4RS. I'm struggling to desperately push it through to some resemblance with R5RS that's already outdated but still usable (see e.g. tinyscheme). Modern R6RS is totally out of reach.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #820 on: September 21, 2014, 11:32:47 AM »
Rob,

If you have (silent) then how can you (unsilent) anything? Or does (silent) mute only the output that is within its scope, e.g. (silent (display "bla bla")) not displaying anything at all?

Quote from: Rob
> (seq 1 22)
Aha! So it constructs a list with values in the range of 1 to 22!
« Last Edit: September 21, 2014, 11:49:30 AM by Mike Lobanovsky »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #821 on: September 21, 2014, 11:39:08 AM »
John,

When OxyLISP is out, it will have its own .PDF mostly based on Arthur's description but with new features added. OxyLISP deserves it. :)

JRS

  • Guest
Re: Lisp in Basic
« Reply #822 on: September 21, 2014, 11:55:16 AM »
That's great Mike. A tribute to both Arthur and Charles.

I'm glad my little QB to SB experiment resulted into something worthwhile. You did a great job of managing that project!

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #823 on: September 21, 2014, 12:21:54 PM »
Thanks John,

But the work isn't complete yet and so far OxyLISP exists solely on my computer. It will become "a project" when it goes public. :)

JRS

  • Guest
Re: Lisp in Basic
« Reply #824 on: September 21, 2014, 12:28:01 PM »
Sounds great!


Where Mike's code really comes from.  :-*