Page 1 of 1

Rosetta's A+B

Posted: Sat Dec 17, 2016 11:18 am
by Dutchman
sarossell introduced smart Basic into "Rosetta".
See viewtopic.php?f=20&t=1664 and http://rosettacode.org/wiki/Category:Smart_BASIC
For me was that website really a revelation. I did not know its existence. Thanks Scott :D
For the task 'A+B' the following code was given:

Code: Select all

INPUT n$
PRINT VAL(LEFT$(n$,(LEN(STR$(VAL(n$))))))+VAL(RIGHT$(n$,(LEN(n$)-LEN(STR$(VAL(n$)))-1)))
END
However, it appears that the code contains links to a website on Qbasic rather than Smart Basic.
Can that be changed to the HTML-page of the SB-manual http://nittersat.ru/BASIC_manual/en.pad ... asics.html :?:

Furthermore sarossell added the following note to his code:
NOTE: This is a horribly forced way of doing this. smart BASIC has commands to SPLIT strings. Surely someone can provide better code than what I've written here. ;@)
I took the chance to try it:

Code: Select all

INPUT n$
SPLIT n$ TO m$,n WITH " "
PRINT m$(0),m$(1),m$(0)+m$(1)
This code gives the desired result.

Re: Rosetta's A+B

Posted: Sat Dec 17, 2016 4:47 pm
by sarossell
YES! That's what I'm talking about! Awesome. I knew somebody would step up and fix that horrible mess. Well done.

As for the qbasic reference, that's a standard descriptor to get Rosetta to recognize BASIC code for context coloring, nothing more. If you can find or create one just for smart BASIC, that would be great. I'm not even sure if it's possible though.

You can always add notes to the entries or comments in the code you feel add to the value of the entry. Please do. :)

Re: Rosetta's A+B

Posted: Sat Dec 17, 2016 4:57 pm
by sarossell
Please feel free to sign up on Rosetta Code and edit the entry for smart BASIC to add your code and any notes. I would prefer leaving existing entries as well to show a progression of improvement. Some people like to call better code solutions "optimized". :)

And spread the word! I think people can really learn a lot about BASIC programming with Rosetta, especially with smart BASIC.

Re: Rosetta's A+B

Posted: Tue Dec 27, 2016 3:52 am
by sarossell
As a former ZX81 programmer with just 1K of RAM back in the early 80s, I picked up the habit of always looking for optimizations in code.

I love the fact that you illustrated perfectly smart BASIC's behavior in treating strings as numeric values when necessary.

A further optimization of your code is to leave out the optional ",n" after the m$ on the SPLIT command making the array size implied by the amount of data provided.

Code: Select all

INPUT n$
SPLIT n$ TO m$ WITH " "
PRINT m$(0),m$(1),m$(0)+m$(1)
P.S.: I went ahead and posted your original code (with proper credit of course) on Rosetta. I've found it to be an excellent learning and teaching web site and look forward to having my future crappy code scrutinized.