User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 4:28 am

This is the GMP extension module running on the Raspberry Pi Zero W. I have attached the gmp.so extension module that should go in /usr/local/lib/scriba directory. The program shown here is also included.

Code: Select all

DECLARE SUB fibo ALIAS "fibo" LIB "gmp"

PRINT LEN(fibo(4784969)),"\n"
pi@raspberrypi:~/sbrpi $ time scriba fibo.sb
1000000

real 0m4.775s
user 0m4.363s
sys 0m0.084s
pi@raspberrypi:~/sbrpi $
Attachments
gmp_zero.zip
(1.84 KiB) Downloaded 62 times

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 4:41 am

Cool.

You are raising the fibo(4784969) challenge to a whole new meta level. Not just calculate fibo(4784969) in the language of your choice but first build a language/compiler/runtime with which to calculate fibo(4784969).

Does your extension actually produce a value that can be used in ScriptBasic? For example

a = fibo(12)
b = fibo(10000)
c = b / a
print c, "\n"

Meanwhile I shaved a few lines off my Scheme fibo and made it almost readable:

Code: Select all

(define (fibo n)
    (cond
        [(equal? n 0) 0 ]
        [(equal? n 1) 1 ]
        [(equal? n 2) 1 ]
        [else
            (define k (quotient n 2))
            (define a (fibo k))
            (define b (fibo (- k 1)))
            (if (even? n)
                (* a (+ (* 2 b) a))
                (let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
                    (if (= (modulo n 4) 1)
                        (+ c 2) 
                        (- c 2) 
                    )
                )
            )    
        ]
    )    
)
Memory in C++ is a leaky abstraction .

ejolson
Posts: 9499
Joined: Tue Mar 18, 2014 11:47 am

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 4:52 am

Heater wrote:
Sun Apr 21, 2019 4:41 am
Cool.

You are raising the fibo(4784969) challenge to a whole new meta level. Not just calculate fibo(4784969) in the language of your choice but first build a language/compiler/runtime with which to calculate fibo(4784969).

Does your extension actually produce a value that can be used in ScriptBasic? For example

a = fibo(12)
b = fibo(10000)
c = b / a
print c, "\n"

Meanwhile I shaved a few lines off my Scheme fibo and made it almost readable:

Code: Select all

(define (fibo n)
    (cond
        [(equal? n 0) 0 ]
        [(equal? n 1) 1 ]
        [(equal? n 2) 1 ]
        [else
            (define k (quotient n 2))
            (define a (fibo k))
            (define b (fibo (- k 1)))
            (if (even? n)
                (* a (+ (* 2 b) a))
                (let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
                    (if (= (modulo n 4) 1)
                        (+ c 2) 
                        (- c 2) 
                    )
                )
            )    
        ]
    )    
)
Hey! I've been doing that too. Did the runtime improve?

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 4:56 am

Yes but lets stay in the range numbers are real to ScriptBasic. Notice even though both fibo() function calls returned strings ScriptBasic was able to do addition without the VAL() function being used.

Code: Select all

DECLARE SUB fibo ALIAS "fibo" LIB "gmp"

PRINT fibo(24) + fibo(24),"\n"
PRINT FORMAT("%.0f",fibo(46)),"\n"
PRINT FORMAT("%.0f",fibo(46) / 16),"\n"
PRINT FORMAT("%.0f",1836311903 / 16),"\n"
jjrs@jrs-laptop:~/sb/examples/test$ time scriba fiboadd.sb
92736
1836311903
114769494
114769494

real 0m0.007s
user 0m0.001s
sys 0m0.006s
jrs@jrs-laptop:~/sb/examples/test$
Last edited by John_Spikowski on Sun Apr 21, 2019 5:11 am, edited 1 time in total.

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 5:10 am

ejolson,
Hey! I've been doing that too.
Sorry. I woke up at an ungodly hour, could not sleep, so started tinkering.
Did the runtime improve?
I was expecting it to get worse. Thinking that "define" gives us a function to call whereas "let" gives a variable to reference. Turns out to be about the same:

Code: Select all

$ time racket  fibo.scheme
10727395641800477229364813596...
...0699706378405156269

real    0m1.631s
user    0m1.031s
sys     0m0.516s
Memory in C++ is a leaky abstraction .

ejolson
Posts: 9499
Joined: Tue Mar 18, 2014 11:47 am

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 6:15 am

Heater wrote:
Sun Apr 21, 2019 5:10 am
ejolson,
Hey! I've been doing that too.
Sorry. I woke up at an ungodly hour, could not sleep, so started tinkering.
Did the runtime improve?
I was expecting it to get worse. Thinking that "define" gives us a function to call whereas "let" gives a variable to reference. Turns out to be about the same:

Code: Select all

$ time racket  fibo.scheme
10727395641800477229364813596...
...0699706378405156269

real    0m1.631s
user    0m1.031s
sys     0m0.516s
It seems I might have ended up with something less readable. Namely,

Code: Select all

#!/usr/bin/racket
#lang racket

(define (fibowork n)
    (cond [(equal? n 0) '(0 1)
    ][else
        (let ([y (fibowork (quotient n 2))]) 
        (let ([a (car y)] [b (cadr y)])
            (cond [(even? n)
                (let ([t1 (- (+ b b) a)])
                (cond [(equal? (modulo n 4) 0)
                    (list (* a t1) (sub1 (* b t1)))
                ][else
                    (list (* a t1) (add1 (* b t1)))
                ]))
            ][else
                (let ([t1 (+ (+ a a) b)])
                (cond [(equal? (modulo n 4) 1)
                    (list (add1 (* a t1)) (* b t1))
                ][else
                    (list (sub1 (* a t1)) (* b t1))
                ]))
            ])
        ))
    ])
)

(define (fiboEJO n)
    (cond [(< n 2) n
    ][else
        (let ([y (fibowork (quotient (- n 1) 2))]) 
        (let ([a (car y)] [b (cadr y)])
            (cond [(even? n)
                (let ([t1 (+ (+ a a) b)])
                    (* b t1))
            ][else
                (let ([t1 (- (+ b b) a)])
                (cond [(equal? (modulo n 4) 1)
                    (sub1 (* b t1))
                ][else
                    (add1 (* b t1))
                ]))
            ])
        ))
    ])
)

(fiboEJO 4784969)
However, it does run a bit faster.

Code: Select all

$ time ./scfiboheat | tail -c32
4856539211500699706378405156269

real    0m1.635s
user    0m1.525s
sys 0m0.106s
$ time ./scfiboejo | tail -c32
4856539211500699706378405156269

real    0m1.465s
user    0m1.348s
sys 0m0.111s
When people code in Scheme with a basic lack of style, why avoid Basic?

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 9:52 am

jalih,
As an old OS/2 user, here is the obligatory REXX version for the fibo(4784969) challenge:
"obligatory"? This is perhaps the first time I have seen anyone post REXX code anywhere!

Great job. Only takes two hours and fifteen minutes to run here. Result is good mind:

Code: Select all

$ time regina fibo.rexx
107273956418004772293648135962250043219072211732...
...211500699706378405156269

real    129m8.833s
user    128m52.891s
sys     0m0.156s
Memory in C++ is a leaky abstraction .

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 10:15 am

ejolson,
When people code in Scheme with a basic lack of style, why avoid Basic?
Style? I ain't got no style. I have never read enough Scheme to know what Scheme style might be. I have not read much Scheme because it's so unintelligible.

Give me any language and I will write it to look as much like C, Pascal, Ada, etc as possible. Then I might have some hope of understanding what I wrote a week later. Luckily Scheme is quite malleable that way.

Anyway, I made some more tweaks in attempt to speed it up a bit but mostly make it look sweet:

Code: Select all

#!/usr/bin/racket
#lang racket

(define memo (make-hash))

(define (memoize n f)
    (hash-set! memo n f)
    f
)

(define (memoized n)
    (dict-has-key? memo n)
)

(define (memoref n)
    (hash-ref memo n)
)

(hash-set! memo 0 0)
(hash-set! memo 1 1)

(define (fiboCalc n)
    (define k (quotient n 2))
    (define a (fibo k))
    (define b (fibo (- k 1)))
    (if (even? n)
        (memoize n (* a (+ (* 2 b) a)))
        (let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
            (if (= (modulo n 4) 1)
                (memoize n (+ c 2)) 
                (memoize n (- c 2)) 
            )
        )
    )    
)

(define (fibo n)
    (if (memoized n)
        (memoref n)
        (fiboCalc n)
    )    
)

(fibo 4784969)
Performance is not quite up to yours:

Code: Select all

$ time ./fibo.racket > /dev/null

real    0m1.427s
user    0m0.828s
sys     0m0.609s
$ time ./fiboejo.racket > /dev/null

real    0m1.395s
user    0m0.891s
sys     0m0.500s
I'll trade the 2% performance hit for comprehensibility.
Memory in C++ is a leaky abstraction .

hippy
Posts: 11907
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 10:50 am

Heater wrote:
Sat Apr 20, 2019 1:36 pm
I'm with the idea of adding instructions to a ISA than are of benefit to VMs in general. I would be appauled if they were tailored to JVM and turned out to not be much use to others.
I wouldn't be worried about extensions tailored to a specific language and no use to anyone else provided they were not a mandatory part of the ISA. To me that's little different to adding extensions which are only of use to those in a specific application domain and of no use outside that.

I believe it would be better for the RISC-V foundation to focus on what is best for the majority than on what's only of benefit for the few but, at the proverbial end of the day, it's their call.

I can see an argument that some extensions might not fit, even go against, the ethos of RISC-V but I would say that ethos should only apply to the core ISA not third-party or vendor extensions. I'm not adverse to RISC-V supporting whole instruction sets of other systems; ARM, X86, JVM, .Net, etc. That would likely only need one extra RISC-V instruction; 'execute that code with this ISA'.

The great thing about RISC-V for me is that it's not limited in any way, that anyone can take it in any direction they would like. Some won't like some of the directions variants of RISC-V may go in but that's not really for them to say. RISC-V is an open, extensible design so one can't really complain when some do take advantage of that.

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 1:06 pm

Style?

From the Racket Style Guide : https://docs.racket-lang.org/style

6.1 Where to Put Parentheses
Racket isn’t C. Put all closing parentheses on one line, the last line of your code.

Oh dear.

Code: Select all

#!/usr/bin/racket
#lang racket

(define memo (make-hash))

(define (memoize n f)
    (hash-set! memo n f)
    f)

(define (memoized n)
    (dict-has-key? memo n))

(define (memoref n)
    (hash-ref memo n))

(hash-set! memo 0 0)
(hash-set! memo 1 1)

(define (fiboCalc n)
    (define k (quotient n 2))
    (define a (fibo k))
    (define b (fibo (- k 1)))
    (if (even? n)
        (memoize n (* a (+ (* 2 b) a)))
        (let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
            (if (= (modulo n 4) 1)
                (memoize n (+ c 2)) 
                (memoize n (- c 2))))))

(define (fibo n)
    (if (memoized n)
        (memoref n)
        (fiboCalc n)))

(fibo 4784969)
How is one supposed to work like that without having seizures?
Memory in C++ is a leaky abstraction .

ejolson
Posts: 9499
Joined: Tue Mar 18, 2014 11:47 am

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 3:17 pm

Heater wrote:
Sun Apr 21, 2019 1:06 pm
Style?

From the Racket Style Guide : https://docs.racket-lang.org/style

6.1 Where to Put Parentheses
Racket isn’t C. Put all closing parentheses on one line, the last line of your code.

Oh dear.

Code: Select all

#!/usr/bin/racket
#lang racket

(define memo (make-hash))

(define (memoize n f)
    (hash-set! memo n f)
    f)

(define (memoized n)
    (dict-has-key? memo n))

(define (memoref n)
    (hash-ref memo n))

(hash-set! memo 0 0)
(hash-set! memo 1 1)

(define (fiboCalc n)
    (define k (quotient n 2))
    (define a (fibo k))
    (define b (fibo (- k 1)))
    (if (even? n)
        (memoize n (* a (+ (* 2 b) a)))
        (let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
            (if (= (modulo n 4) 1)
                (memoize n (+ c 2)) 
                (memoize n (- c 2))))))

(define (fibo n)
    (if (memoized n)
        (memoref n)
        (fiboCalc n)))

(fibo 4784969)
How is one supposed to work like that without having seizures?
Hooray! Basically, I like your new Scheme. The parenthesis look less like worms when grouped together. It's amazing that Basic, Scheme and Python were each designed for teaching.

Now, to keep up with the meta challenge, who will code a Basic in Scheme or alternately Scheme in Basic?

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 3:25 pm

Now, to keep up with the meta challenge, who will code a Basic in Scheme or alternately Scheme in Basic?
I'll see if I can get the Tiny Scheme extension module I wrote for ScriptBasic compiled for the RPi.

A while back a friend and I converted a LISP that was written in QuickBasic to ScriptBasic. It was the largest ScriptBasic program I ever wrote. (3K + lines if I remember correctly) I'll see if the repo is still available for it.

Here is the QB Lisp to ScriptBasic repo.
https://bitbucket.org/ScriptBasic/basic ... rc/master/

We didn't discover the hidden POP ScriptBasic keyword till after it was converted. It would made this conversion much easier than converting some of the GOSUBs to functions. I have no plans to revisit this and moved on to the TinyScheme extension module.

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 5:21 pm

ejolson,
Hooray! Basically, I like your new Scheme
Why thank you. I guess I learned something whilst nodding off in front of Brian Harvey's lectures on youtube.
The parenthesis look less like worms when grouped together
This I don't understand. My gut has the totally opposite reaction.

At the end of the day, no matter what language you are using, we only have sequence, selection and iteration. It feels more natural that if you are going to interrupt a sequence with a selection, say, then the sub-sequences should be indented to clearly show that, the parenthesis should be balanced with that indentation. Otherwise you are lost in a sea of worms.
It's amazing that Basic, Scheme and Python were each designed for teaching.
Yes it is. Why would one inflict such torture on ones children?
Now, to keep up with the meta challenge, who will code a Basic in Scheme or alternately Scheme in Basic?
I have to take some time to evaluate that circular meta challenge. (Get it :))

With any luck we can abstract things away far enough that we no longer need any hardware to run it on.
Memory in C++ is a leaky abstraction .

ejolson
Posts: 9499
Joined: Tue Mar 18, 2014 11:47 am

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 5:49 pm

hippy wrote:
Sun Apr 21, 2019 10:50 am
RISC-V is an open, extensible design so one can't really complain when some do take advantage of that.
I think the R means it's reduced; otherwise, it should have been called the EISC-MMXIX. Do you think the EISC-MMXX could add a Fibonacci instruction?

Although it is often legal for the code of a smaller open-source project to be assimilated into a larger one, there is a danger when such things happen that the original project is discarded. However, code which solves one problem well often has fewer bugs than an all-encompassing framework.

The same holds true with hardware. From my point of view, RISC-V is as much a reaction to all the errata in existing processor designs as it is an architecture engineered to make efficient use of a transistor budget now constrained by fundamental laws of physics. Still, as you've mentioned, another strength of RISC-V is that it is freely licensed to be adapted for many uses.

For example, I fully expect RISC-V to be assimilated into systemd as a custom coprocessor necessary for faster booting. Since the only alternative way of booting a Linux system will involve an emulator written in Basic, running systemd will eventually be the dominant use for RISC-V hardware. This in turn will become the only use--all to avoid Basic.

timrowledge
Posts: 1408
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 6:06 pm

Heater wrote:
Thu Apr 18, 2019 3:56 am
Tim,

So I downloaded from squeak.org. It's a bit worrying that their latest source code is from 2012 !!!
From here: http://squeakvm.org/unix/
I'd be interested how you were lead from downloading a complete package from squeak.org to grabbing anything from the outdated and usused squeakvm.org. If there is a bad description we need to fix it.
What made you think that you needed to download the sources to a VM and build in the first place? The packages include a suitable VM in every case I can see. And since I've pointed out that the system is under active development I'm puzzled why you might imagine that sources from 2012 might have anything to do with it.
The big surprise is that it seems to be impossible to turn 10 lines of Smalltalk into a running program and get a result without an IDE. Except possibly for GNU Smalltalk which seems to be broken.
It works perfectly well for me on Raspbian, and Ubuntu 18 running on an AWS thing, and Mac OS, I even held my nose and fired up Windows 10 in my VMWare stuff and tested it there; works fine except that for some reason Windows doesn't talk to stdio. Since I don't do the vm development (or anything else if at all possible) on Windows I can't suggest any reason for that other than, well, Windows.
It (specifically the .cs file I posted) works fine as an argument from the commandline, as a file dropped on the Squeak desktop or manually filed in with the FileBrowser tool.
And yet it has a very typical looking menu bar at the top, with a fairly common logo item at the left top corner, that provides a menu which allows saving and exiting.
Yes it does. Except none of that shows up or if it does it does not respond after Squeak has crashed. Which has done pretty quickly on every run so far. Requiring it be killed from the Task Manager.
And yet on the aforementioned VMWare/Windows test I note that it does indeed quit if you click on the window close button. And it even nicely asks if you mean it, like a well behaved system should.
I have no idea what you are doing or how your machine is configured such that Squeak crashes. I've been doing this a while (like 35 years) and it really doesn't break very often outside of experimental VM work. My Squeak based weather machine system has been running continuously on a Pi 2 for several years. Some of the Squeak servers have uptime exceeding 5 years.
No, I'm trying to run 10 lines of code on a computer. I don't want a tool box, I just want to run that code from the source I have.
You get a whole stack of toolboxes by having an OS. To run the C source you have to use a vast array of complex tools that occupy mega(or even gigag)bytes of disk space. Just because those tools don't open windows doesn't mean they aren't there. In the case of Squeak Smallkalk it happens to default to opening a window in the normal case. It is certainly possible to change the system image to not do that and for some purposes people have done just that. It's just not interesting enough to enough people for much effort to have been put into it. If you can make a case for it that enough people would be interested in it enough to spend the time working on it, give it a go.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

User avatar
bensimmo
Posts: 5593
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 7:16 pm

Why not just use the Pi, Squeaks on there in the Desktop download by default.
Seems an easier way to play with it.

You could even add it to Scratch ?
Or try do it all in Scratch?

Edit. A funny autocorrect.
Last edited by bensimmo on Mon Apr 22, 2019 8:06 am, edited 1 time in total.

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 7:27 pm

timrowledge,
I'd be interested how you were lead from downloading a complete package from squeak.org to grabbing anything from the outdated and usused squeakvm.org.
Am I losing my mind or are you?

On page 70 of this thread you said:

"Oh, yuck. We really have to get this removed from Debian or make them actually include it properly. They completely and utterly failed to .. .oh, never mind. It's rubbish. Don't use that route.
Download from squeak.org."


So I tried to do as you instructed. (Actually I'm not sure how the squeakvm.org thing crept in here. It's not even a site I can get too.)

What I have here now on my Debian Linux machine is Squeak5.2-18229-64bit-All-in-One.zip which is what squeak.org serves up as the current release. I just verified that by checking the md5sum of what I used vs what I get now from squeak.org.

I then run it as you directed:
sudo ./Squeak5.2-18229-64bit-All-in-One.app/Contents/Linux-x86_64/bin/squeak ./Squeak5.2-18229-64bit-All-in-One.app/Contents/Resources/Squeak5.2-18229-64bit.image ./bigFib.cs
It fails with the error I reported previously.

What to do?
If there is a bad description we need to fix it.
Yes please.

With all due respect Tim you have written a lot there and said nothing. I'm happy it has worked for you for many years. I want it to work for me as well.

This cannot be so hard. All I need is a few pieces of information:

1) Where to download a working Smalltalk system for my 64 bit Intel Debian 9 Linux box from (With X Windows for all the GUI goodness).

2) How to install and run it. If there is anything I need to know that is not in the docs in the download.

3) How to run your BigFib code.

4) Later we can talk about how to get my own fibo code working.

You may have noticed I have had to deal with getting a bunch of language systems running here on this thread, from ALGOL 60 to Scheme to Haskell etc. I have installed an used all kind of languages, run-times, cross compilers etc over the years. I don't want to let Smalltalk defeat me.

Any advise is welcome.
Memory in C++ is a leaky abstraction .

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 7:31 pm

bensimmo,
Why not just use the Pi, Squeaks on there in the Desktop bedroom by default.
Good question.

The only Pi I have to hand is a headless system. It's running a 64 bit Debian, pi64. I don't believe there is a Squeak for 64 bit ARM.
You could even add it to Scratch ? Or try do it all in Scratch?
No. Just no.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 7:34 pm

I couldn't get your fibo to display a result so here is a ASCII Mandelbrot just to show concept. All display output from TinyScheme is return as string to ScriptBasic.

Code: Select all

DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"

sc = InitNew()
LoadStr(sc, "(load \"init.scm\")")
mandel = """
(newline)
(display "Ascii Mandelbrot TinyScheme") (newline)
(display "---------------------------") (newline)
(newline)

(define sq
   (lambda (x) (* x x)))

(define (1+ x) (+ x 1))
(define (1- x) (- x 1))

(define level
  (lambda (i x y rc ic it orb)
   (if (or (= i it) (> orb 4)) i
    (level (1+ i) (+ rc (- (sq x) (sq y))) (+ ic (* 2 x y)) rc ic it (+ (sq x) (sq y))))))


(define sq
   (lambda (x) (* x x)))

(define (1+ x) (+ x 1))
(define (1- x) (- x 1))

(define level
  (lambda (i x y rc ic it orb)
   (if (or (= i it) (> orb 4)) i
    (level (1+ i) (+ rc (- (sq x) (sq y))) (+ ic (* 2 x y)) rc ic it (+ (sq x) (sq y))))))

(define mlevel
   (lambda (L)
     (level 0 (cadr L) (car L) (cadr L) (car L) 11 0)))

(define (main)
   (let ((cnt 0) (lvl 0) (xo -1.7) (yo -2.3) (dz 0.1) )
     (do ((i 0 (1+ i)))
         ((= i 30))
        (do ((j 0 (1+ j)))
            ((= 30 j))
              (set! lvl (mlevel (list (+ xo (* i dz)) (+ yo (* j dz)) )))
              (if (< lvl 10)
                   (begin (display lvl) (display " "))
                   (display lvl))
              (set! cnt (1+ cnt))
              (when (= 30 cnt)
                 (set! cnt 0)
                 (newline))
))))

(main)
(quit)
"""

PRINT LoadStr(sc, mandel),"\n"
Deinit sc
Output

Code: Select all

jrs@jrs-laptop:~/sb/examples/test$ time scriba mandelisp.sb

Ascii Mandelbrot TinyScheme
---------------------------

1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 2 2 2 2 2 2 2 2 2 
1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 
1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 115 4 4 3 3 2 2 2 
1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 5 7 9 114 4 3 3 2 2 
1 1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 9 118 5 4 4 3 3 3 
1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 8 1111116 5 5 4 3 3 
1 1 1 1 1 2 3 3 3 3 3 3 3 4 4 4 5 7 8 8 101111119 6 6 5 4 3 
1 1 1 1 2 3 3 3 3 3 3 3 4 4 5 5 6 11111111111111111111114 3 
1 1 1 1 2 3 3 3 3 3 4 5 5 5 5 6 8 111111111111111111117 5 3 
1 1 1 1 3 3 3 3 4 5 7 7 7 7 7 7 11111111111111111111119 5 4 
1 1 1 1 3 4 4 4 5 5 7 111111119 1111111111111111111111116 4 
1 1 1 1 4 4 4 5 5 6 8 11111111111111111111111111111111115 4 
1 1 1 1 4 4 6 6 7 1111111111111111111111111111111111118 5 4 
1 1 1 1111111111111111111111111111111111111111111111117 5 4 
1 1 1 1 4 4 6 6 7 1111111111111111111111111111111111118 5 4 
1 1 1 1 4 4 4 5 5 6 8 11111111111111111111111111111111115 4 
1 1 1 1 3 4 4 4 5 5 7 111111119 1111111111111111111111116 4 
1 1 1 1 3 3 3 3 4 5 7 7 7 7 7 7 11111111111111111111119 5 4 
1 1 1 1 2 3 3 3 3 3 4 5 5 5 5 6 8 111111111111111111117 5 3 
1 1 1 1 2 3 3 3 3 3 3 3 4 4 5 5 6 11111111111111111111114 3 
1 1 1 1 1 2 3 3 3 3 3 3 3 4 4 4 5 7 8 8 101111119 6 6 5 4 3 
1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 8 1111116 5 5 4 3 3 
1 1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 9 118 5 4 4 3 3 3 
1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 5 7 9 114 4 3 3 2 2 
1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 115 4 4 3 3 2 2 2 
1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2

real	0m0.755s
user	0m0.753s
sys	0m0.001s
jrs@jrs-laptop:~/sb/examples/test$ 
Here is the traditional Fibonacci in TinyScheme.

Code: Select all

DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"

sc = InitNew()
LoadStr(sc, "(load \"init.scm\")")

fibolisp = """
(define fibonacci (lambda (n)
  (if (< n 2)
      n
      (+ (fibonacci (- n 1))
         (fibonacci (- n 2)))))
)
(display (fibonacci 24))
"""
PRINT LoadStr(sc, fibolisp)
Deinit sc
jrs@jrs-laptop:~/sb/examples/test$ time scriba fibolisp.sb
46368
real 0m1.266s
user 0m1.258s
sys 0m0.008s
jrs@jrs-laptop:~/sb/examples/test$

Let me know if this is worth the effort to create a Raspberry Pi Zero extension module.
Last edited by John_Spikowski on Sun Apr 21, 2019 8:33 pm, edited 3 times in total.

Heater
Posts: 19056
Joined: Tue Jul 17, 2012 3:02 pm

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 7:50 pm

Tim,

Sorry if the above sounded overly grumpy but this is very frustrating. I'm getting blocked at every turn.

I just double checked. What I'm running on Windows is also an up to date Squeak from squeak.org. It does not work:

Code: Select all

...Squeak5.2-18229-64bit-201810190412-Windows>Squeak.exe Squeak5.2-18229-64bit.image bigFib.cs 
"Error: stdout is unavailable"

I think you acknowledged that issue above.

At this point I'd be happy to see your fibo run and see the output in the Squeak transcript window.

If I can figure out how to get my own code running in their I'd be over the moon.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 8:22 pm

AIR@AllBASIC.info wrote: GO implementation:

Code: Select all

package main

import (
    "fmt"
    "math/big"
)

func Mul(x, y *big.Int) *big.Int {
    return big.NewInt(0).Mul(x, y)
}
func Sub(x, y *big.Int) *big.Int {
    return big.NewInt(0).Sub(x, y)
}
func Add(x, y *big.Int) *big.Int {
    return big.NewInt(0).Add(x, y)
}

func fib(n int) (*big.Int, *big.Int) {
    if n == 0 {
        return big.NewInt(0), big.NewInt(1)
    }
    a, b := fib(n / 2)
    c := Mul(a, Sub(Mul(b, big.NewInt(2)), a))
    d := Add(Mul(a, a), Mul(b, b))
    if n%2 == 0 {
        return c, d
    } else {
        return d, Add(c, d)
    }
}

func main() {
    a, _ := fib(4784969)
    fmt.Println(a)
}
Output

Code: Select all

Without printing the result

real    0m0.352s
user    0m0.342s
sys    0m0.013s[/tt]

With print

[tt]real    0m2.632s
user    0m2.597s
sys    0m0.021s

Test System:

system_profiler SPHardwareDataType
Hardware:

    Hardware Overview:

      Model Name: Mac mini
      Model Identifier: Macmini6,2
      Processor Name: Intel Core i7
      Processor Speed: 2.3 GHz
      Number of Processors: 1
      Total Number of Cores: 4
      L2 Cache (per Core): 256 KB
      L3 Cache: 6 MB
      Memory: 16 GB
      

ejolson
Posts: 9499
Joined: Tue Mar 18, 2014 11:47 am

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 9:26 pm

Heater wrote:
Sun Apr 21, 2019 7:31 pm
You could even add it to Scratch ? Or try do it all in Scratch?
No. Just no.
I would greatly enjoy seeing that scratchy orange cat open it's mouth and utter the famous million-digit Fibonacci number. Given the TIOBE popularity of Scratch, I expect there are many who would be able to write (or click, drag and drop) the code. However, since Scratch apparently can't do file input or output, I'm not sure who would check that all the digits are correct.

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 10:48 pm

TinyScheme (LISP) Extension Module

I built a TinyScheme extension module for the Raspberry Pi Zero (runs on all RPi boards) if you would like to give it a try. I'm not much of a LISP programmer so any help with syntax will need to come from forum users.

* Unzip to a tmp directory.
* Copy libtinyscheme.so to your /usr/lib/ directory.
* Copy ts.so to your /usr/local/lib/scriba/ directory.
* Copy the remaining examples and init.scm to your ScriptBasic examples directory where ever that may be.

Note: The init.scm file is a Lisp script that adds additional syntax to your Lisp scripts.

TinyScheme Manual

Using Lisp dynamically from strings you create in ScriptBasic makes it an Interesting language extension. Lisp scripts can also be loaded from disk on the fly as well.

Feedback welcome!
Attachments
TinyScheme_RPi_Zero.zip
(84.67 KiB) Downloaded 64 times
Last edited by John_Spikowski on Sun Apr 21, 2019 11:43 pm, edited 4 times in total.

ejolson
Posts: 9499
Joined: Tue Mar 18, 2014 11:47 am

Re: Why Avoid BASIC on RPi?

Sun Apr 21, 2019 11:02 pm

ejolson wrote:
Fri Apr 19, 2019 12:36 am
I looked for Yet Another Fibonacci Language and found GNU bc the Basic Calculator.
Slight modifications of the GNU Basic Calculator Fibonacci code yielded a program compatible with the version of Basic Calculator included with Research Unix Release 7 as distributed by Charles Haley and Dennis Richie in 1979. The modified code looks like

Code: Select all

define f(n){
    if(n==0){
        a=0; b=1;
        return(a);
    }
    y=f(n/2);
    if(n%2==0){
        t=2*b-a; a=a*t; b=b*t;
        if(n%4==0) {
            b=b-1; 
            return(a); 
        }
        b=b+1;
        return(a);
    } 
    t=2*a+b; a=a*t; b=b*t;
    if(n%4==1){ 
        a=a+1; 
        return(a); 
    }
    a=a-1;
    return(a);
}
f(7499);
quit;
As the computers on which Unix R7 ran had a 16-bit address space, only the 7499th Fibonacci number was computed. When run on real PDP-11/70 hardware the results are

Code: Select all

$ time bc fibo.bc >fibo.txt

real       30.0
user       23.0
sys         3.0
$ cat fibo.txt
706039879937678439008043140315376240713352339053705987810495062278638\
8718825722561078363937111808052845829043934364899297524579596771135537\
4801002080270853698968059366022272185817616026077096023349398894847983\
9714294221838897832623108356114939220536986382598957620755114616454599\
1161518248812602059748659792822539047900440615827674362997814592886203\
6233871777454827819617640377892562042118765168677553893278624710989272\
0750453526082811016759670376082246298871700521370426055551726269306739\
0672579278642451139772159098237484766060153417966500228867893493613281\
8993979198118179855263939840784481752316021163437326695353825928661768\
3002190127657350138849014296026957342968901034215368495591044620609723\
6075006861770107503447313897585687337597851533416564601034740128818628\
8645591663416538717475614601835764250489591524740065243768671081547198\
2672495157781105852040979589027195320750103738958083706562749130366493\
9319008074326639606500449899956261137209763009997639133968161102379352\
1005265212193190096689493605090171903058222057356370475825968443610545\
3657010557565875895812678161531478689730791188599192215196196394651362\
1905753392384260111071739837465056829372023129875545405507382500246527\
4343153922152993044195779337044156381692291477614825519892105365222191\
0649719893063976464871428717392844399965747793496261915059129980848401\
5221733892947047898044789624388717645430081789847014505889073245674298\
2274890606022749963374408459031409736132841733310512973055666695851121\
5784820984339191265679662767763463393454227116481221490807304074849241\
9868112329633702241675255001
Note that the answer agrees with previous results obtained by running a line-numbered BASIC program on a simulated Altair 8800 posted earlier. Strangely, the speed is similar, too. This is likely because the simulated Altair is much faster than a real one would have been. I wonder how fast this retro 8-bit computer could compute the 7499th Fibonacci number. It seems all those old computers are full of new life. Happy Easter!

User avatar
John_Spikowski
Posts: 1614
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA

Re: Why Avoid BASIC on RPi?

Mon Apr 22, 2019 1:41 am

This RPI Zero example better shows how ScriptBasic interacts with the TinyScheme extension module. It seems that TinyScheme likes a <return> character to end the command string your sending. The Fibonacci script runs twice as fast as before I added the <return> to the commands. I was also seeing occasional seg faults for no reason. I may try to rebuild the libtinyscheme.so with the latest version.

Code: Select all

DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"

sc = InitNew()
fibolisp = """
(define fibonacci (lambda (n)
  (if (< n 2)
      n
      (+ (fibonacci (- n 1))
         (fibonacci (- n 2)))))
)

"""
ok = LoadStr(sc, fibolisp)
FOR i = 1 TO 24
  results = LoadStr(sc, "(display (fibonacci " & STR(i) & " ))\n")
  PRINT results,"\n"
NEXT
Deinit sc
pi@raspberrypi:~/sbrpi $ time scriba fibolisp.sb
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368

real 0m10.832s
user 0m9.137s
sys 0m0.030s
pi@raspberrypi:~/sbrpi $
Last edited by John_Spikowski on Mon Apr 22, 2019 2:21 am, edited 3 times in total.

Return to “Off topic discussion”