Author Topic: Lisp in Basic  (Read 208368 times)

0 Members and 2 Guests are viewing this topic.

JRS

  • Guest
Re: Lisp in Basic
« Reply #600 on: August 20, 2014, 06:20:14 PM »
Quote
Concerning "GOTO to another GOSUB", it wouldn't be a proper vision of the problem. You may not goto to another sub freely while in recursion. For you to have the right to goto, the both ends of the jump must be at the same recursion depth level or the stack will get disbalanced sooner or later, which will end up in an inevitable crash.

I'm confused. Are you speaking of SBLisp's stack or Script BASIC's stack?

« Last Edit: August 20, 2014, 06:27:34 PM by John »

JRS

  • Guest
Re: Lisp in Basic
« Reply #601 on: August 20, 2014, 06:22:55 PM »
Quote
I confirm Mike's timing with o2 asm.

Damn open source source software cheats!  ;D

JRS

  • Guest
Re: Lisp in Basic
« Reply #602 on: August 20, 2014, 09:33:57 PM »
GNU CLISP 2.49 Scratch pad. CompileOnLine.com is running on CentOS (Red Hat clone) 64 bit. Script BASIC is also available there with extension module support. (cURL, SQLite3, ...)


.
« Last Edit: August 20, 2014, 10:45:05 PM by John »

JRS

  • Guest
Re: Lisp in Basic
« Reply #603 on: August 20, 2014, 10:22:52 PM »
How would I do this in SBLisp?

Code: [Select]
(loop for i from 1.0 to 3.0 by 0.5 do (print i))

1.0
1.5
2.0
2.5
3.0

JRS

  • Guest
Re: Lisp in Basic
« Reply #604 on: August 20, 2014, 11:18:09 PM »
SBLisp working on CompileOnLine.com.


.

Charles Pegge

  • Guest
Re: Lisp in Basic
« Reply #605 on: August 21, 2014, 01:11:58 AM »

Optimised Recursive Ackermann ( o2 asm)

reduces use of stack:

Code: [Select]
function AckB(sys m,n) as sys

mov ecx,m
mov edx,n
return call A

.A
(
  cmp ecx,0
  jg exit       'm>0 skip
  inc edx       'n+1
  mov eax,edx   'ret n+1
  ret
)
(
  cmp edx,0     '
  jg exit       'n>0 skip
  dec ecx       'm-1
  mov edx,1
  call A        'A m-1,1
  ret
)
dec edx         'm,n-1
push ecx        'save m
call A          't
pop ecx         'm reload
dec ecx         'm-1
mov edx,eax
call A          'A m-1, t
ret

end function

AckB 4,1 '65533

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #606 on: August 21, 2014, 02:23:58 AM »
Some more good news ;

Local functions do work

D:\>sblisp
SBLisp - Scheme BASIC Lisp

0](define tst
1]   (lambda (x y)
2]     (let ((f (lambda (x y) (+ x y))))
3]       (f x y))))
TST
0](tst 6 7)
13
0](define tst2
1]  (let ((f (lambda (x y) (+ x y))))
2]   (lambda (x y)
3]    (f x y))))
TST2
0](tst2 5 5)
10
0](tst2 5 56)
61
0] :)

----------------------------------- 
this makes constructions as follows possible

(define max
  (let ((max0 (lambda (x y)
             (if (> x y) x y))))
     (lambda (i L)
       (if (< (length L) 1) i
           (max (max0 i (car L)) (cdr L))))))

-- the primitive is local inside the recursive definiton

but there is more :

 8)

(define variadic
   (lambda (init op L)
      (if (< (length L) 1) init
        (variadic (eval (list op init (car L))) op (cdr L)))))

 ;)

testing
(define max0 (lambda (x y)
             (if (> x y) x y)))
MAX0
(define variadic
   (lambda (init op L)
      (if (< (length L) 1) init
        (variadic (eval (list op init (car L))) op (cdr L)))))
VARIADIC
0](variadic 'max0 '(7 88 8))
ERROR: Too few args.
0](variadic 0 'max0 '(7 88 8))
88
0](variadic 0 'max0 '(7 88 8999))
8999
0](variadic 0 'max0 '(79999 88 8999))
79999
0]

(variadic initial-value operator List)   expands any operator defined with two arguments from left to right over a list !!!

best Rob




 

best Rob 


.
« Last Edit: August 21, 2014, 03:39:48 AM by RobbeK »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #607 on: August 21, 2014, 03:59:58 AM »
How would I do this in SBLisp?
Code: [Select]
(loop for i from 1.0 to 3.0 by 0.5 do (print i))

Hi,

Again far from Rob's natural elegance but passable for your immediate needs:

Code: [Select]
; ===================================
;;; Rob's macros
; ===================================
(define make-listx
     (lambda (i x L)
      (if (= i x) L
          (make-listx (+ i 1) x (cons i L)))))

(define range
  (lambda (x)
    (reverse (make-listx 0 x '() ))))

(define iterate
  (lambda (op it)
    (map (eval op) (range it))
  T ))

(define println
   (lambda (s)
     (print s) (newline)))


; ===================================
;;; My FOR/TO/STEP/EXEC-COMMAND macro
; ===================================
(define iter 0)
(define step 0)
(define comm 0)

(define exec-for
  (lambda ()
    (comm)
    (set! iter (+ iter step))
  )
)

(define for
  (lambda (f u s c)
    (set! iter f) (set! step s) (set! comm c)
    (iterate 'exec-for (floor (+ (/ (- u f) s) 1)))
  )
)
; ===================================

; NB: ITER is the name reserved above for the FOR iterator

(define print-i (lambda () (println iter))) ; define what we want to do
(for 1 3 0.5 print-i) ; do it

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #608 on: August 21, 2014, 05:06:23 AM »
Nice Mike,

I'll work out a much "smarter" iterator ..

we have now
(range . )   indexbase 0
(seq . )  indexbase 1
maybe I should make a generalisation (index start end step) .  .. yep, I'll do this (but keep the range and seq )



0](define n!
1]   (lambda (x)
2]    (apply * (seq x))))
N!
0](n! 5)
120
0] ;)

best Rob

Mike Lobanovsky

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

maybe I should make a generalisation (index start end step) .
Great! You'll become John's hero if you do. :)

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #610 on: August 21, 2014, 06:04:15 AM »
Quote
Concerning "GOTO to another GOSUB", it wouldn't be a proper vision of the problem. You may not goto to another sub freely while in recursion. For you to have the right to goto, the both ends of the jump must be at the same recursion depth level or the stack will get disbalanced sooner or later, which will end up in an inevitable crash.

I'm confused. Are you speaking of SBLisp's stack or Script BASIC's stack?

I'm theorizing out loud. You can't GOTO to a label that expects a GOSUB because you'll eventually hit a RETURN that'll cause a runtime error. Similarly you can't GOSUB into a GOTO label unless it ends up in an END command.

RETURN is a command that exits from the current GOSUB unwinding the process stack of the engine (SB or FBSL or O2, whatever) by one call stack frame, that is by one recursion depth level if recursion is taking place or by one nested call level if the call has been made to another GOSUB label. It then resumes execution from the line that follows the GOSUB line.

QB's RETURN Label implements an interesting functionality that combines two steps in one. First it returns as an ordinary RETURN described above. However it doesn't then resume execution from the line that follows but instead GOTO-es all by itself to the label denoted by Label. Thus you can override the resumption line without giving a damn where it actually is and instead jump to another GOTO label anywhere in the top-level code -- and HandleError is a top-level GOTO label in the QB code of Lisp-in-Basic. And this phenomenon repeats on and on until BSD reaches zero, which means all levels of nesting/recursion have been unwound and the code pointer may safely GOTO to the main loop labeled LispReadEvalPrintLoop.

Does that make sense now?

Until you realize how the QB code really works, you won't be able to reimplement its functionality in full with somewhat different means at hand in SB, FBSL, and O2. You should understand that I'm vocalizing all these things only to keep you informed of what's going on in XBLisp code and why. If I were alone on this project, I would've been doing all my work in complete silence.
« Last Edit: August 21, 2014, 06:12:50 AM by Mike Lobanovsky »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #611 on: August 21, 2014, 06:17:22 AM »
Optimised Recursive Ackermann ( o2 asm)
reduces use of stack:

Bravo Charles!

How could I be so obviously stupid to reload those two registers over and over for nothing? It cost me extra 3.5 seconds spent on nothing! :-[

Thanks a lot for the fix! :)

.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #612 on: August 21, 2014, 07:07:06 AM »
Mesdames et Messieurs ... ... Cached Ackermann in DynC!

Tada-a-a-a-a...!!! ;D

That's a copy-paste of John's ANSI C snippet:

.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #613 on: August 21, 2014, 07:38:18 AM »
Great! You'll become John's hero if you do  ...

à votre service, Michel  :)   here it is ... the smooth iterator  https://www.youtube.com/watch?v=cA9gUspn6gc

(define make-indexx
     (lambda (i end step m)
      (if (> i end) (reverse m)
          (make-indexx (+ i step) end step (cons i m)))))

(define iterate+
    (lambda (op i end step)
      (map (eval op) (make-indexx i end step '() )) T ))

;; in action /test


0]
0](define g (lambda (x) (println (* x x))))
G
0](iterate+ 'g 1 4 0.5)      ;;;;    operator ,  start , end , step
1
2.250000
4
6.250000
9
12.250000
16
T
0]

(the condition is still true if   index = end  , here 4²  -- keep it or make it >= is false

I will make iterate-  too for the "downsteps"

best, Rob

(pudates always included in the attached macro.scm file   

.
« Last Edit: August 21, 2014, 07:56:58 AM by RobbeK »

JRS

  • Guest
Re: Lisp in Basic
« Reply #614 on: August 21, 2014, 08:09:39 AM »
Very cool Rob!

Let me know if you need any help testing the new BASIC interpreter written in SBLisp8) ;D