RetroBASIC
Basicprogramming(.org) => Code and examples => Topic started by: B+ on June 19, 2016, 06:50:57 PM
-
Tomaaz
The whole challenge is completely pointless. What about something like this:
Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name.
Perl
Code: [Select]
print for (map {sin() (1..100)})
How many other languages can do it?
'one statement sin table.bas SmallBASIC 0.12.6 [B+=MGA] 2016-06-19
'using variables allows going beyond integers on one statement SIN table
for i in seq(0, 2*pi, 360/15+1) do ? round(deg(i)), ((10000*sin(i))\1)*.0001
-
New and improved:
for i in seq(0,2*pi,360/15+1) do ?round(deg(i)),round(sin(i),4)
Round has decimal place option.
Thanks jsalia
-
Well heck, here is a trig table:
'one statement trig table.bas SmallBASIC 0.12.6 [B+=MGA] 2016-06-19
'using variables allows going beyond integers on one liner trig table
for i in seq(0,2*pi,360/15+1) do ? using "##0.0000 ";deg(i),sin(i),cos(i),iff(cos(i)<>0,tan(i),999.9999)
-
Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name.
This one was hard to swallow. But very interesting.
My approach is by using direct memory operations. First we have to find a suitable memory address. This is the hardest part, because usually, a memory address resides in a variable.
The trick is using a memory address to a function which always exists. In BaCon, code is being translated to C, and therefore, there always is a function called 'main'.
We can refer to its memory address by using '&main'. Note that the construct '&main' does not refer to a variable, but to the address of a function with the actual name 'main'.
The below program simply refers to the memory address of the 'main' function to POKE and PEEK values. Whatever was there, at the point of the first POKE, we've already past it.
Regards
Peter
' First we need to unprotect the memory where this program is located
PRAGMA INCLUDE <sys/mman.h>
CALL mprotect(&main-MOD(&main, getpagesize()), getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC)
' Initialize the memory address of function 'main' to 1
POKE &main, 1
' Loop until we have reached 100
REPEAT
PRINT SIN(RAD(PEEK(&main)))
POKE &main, PEEK(&main)+1
UNTIL PEEK(&main) = 100
-
Newlisp:
(map println (map sin (sequence 1 1000 0.1)))
Not limited to integers. For integers only, Perl is pretty unbeatable. ;)
print sin for(1..1000);
-
In BaCon I can do floating values too, see below for the same range from 1-1000 step 0.1. But my code isn't getting any smaller... ;)
' First we need to unprotect the memory where this program is located
PRAGMA INCLUDE <sys/mman.h>
CALL mprotect(&main-MOD(&main, getpagesize()), getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC)
' Set the way we look at memory
OPTION MEMTYPE float
' Initialize the memory address of function 'main' to 1
POKE &main, 1
' Loop until we have reached 1000
REPEAT
PRINT SIN(RAD(PEEK(&main)))
POKE &main, PEEK(&main)+0.1
UNTIL PEEK(&main) >= 1000
-
ok
print sin for(1..1000);
is that work without two points ?
(1..1000)
i think not ..so this is just ordinary predefined for-Loop
-
For integers only, Perl is pretty unbeatable.
Well, it turns out that the 'map' (http://perldoc.perl.org/functions/map.html) function in Perl uses an anonymous variable (http://perldoc.perl.org/perlvar.html#SPECIAL-VARIABLES) behind the scenes.
So in the end, it uses a variable after all, we could consider this cheating....? :)
-
Well, it turns out that the 'map' (http://perldoc.perl.org/functions/map.html) function in Perl uses an anonymous variable (http://perldoc.perl.org/perlvar.html#SPECIAL-VARIABLES) behind the scenes.
So in the end, it uses a variable after all, we could consider this cheating....? :)
print sin for(1..1000) doesn't use map. ;) But, you're right - it uses the $_ variable which is used by default (you don't need to type it). And the original task was Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name. ;) To be honest I posted it as an example of a task that is to specific and constructed the way that makes certain language (Perl in this case) an automatic winner. But, was I right? I'm not sure anymore. ;)
ok
print sin for(1..1000);
is that work without two points ?
(1..1000)
i think not ..so this is just ordinary predefined for-Loop
Yes, it is. What's wrong with that? Did anyone say that loops were not allowed? :) (1..1000) is a range of integers between 1 and 1000. You could also write it like that:
for [$_] (1..1000) {
print sin([$_]);
}
$_ is used by default if the other variable name is not specified, so you don't have to write it. Unfortunately, ranges in Perl don't allow to define step, so, to get sinuses of numbers from 1 to 1000 step 0.1, you would have to write something like this:
print sin for (map {$_ / 100} (1..100000))
I thik that in this case Newlisp wins. ;)
Okay, I get it now. SIN() range with no variables used.
Have fun!
Any task that ScriptBasic fail to complete is automatically a stupid one? ;)
-
For Tomaaz original challenge, I gave up doing it without a variable and was happy to be able to do a whole trig table in one statement (not one line with : separating statements, just one statement) which so happens to be the title of this thread. "One statement SIN table"
I tried to print out a more meaningful table incremented by fractions of circle instead of Radian values incremented by 1,
because I realized Tomaaz would be harder put trying to do it without integers but apparently he found something for that too! ;) (but with pi?)
Ha, I never expected Peter to focus on the the no variable part! Whew!!! awesome.
As John says, "Have fun!"
-
There is even a more simple way to achieve this goal by using the TIMER function. This function keeps track of the amount of milliseconds a program is running. If your favorite BASIC has a similar function then it should work there too.
REPEAT
SLEEP 10
PRINT SIN(RAD(TIMER/10))
UNTIL TIMER/10 > 100
PS here TIMER is not a variable. Note that in BaCon, functions without arguments are written without parenthesis. So it is TIMER and not TIMER().
-
There is even a more simple way to achieve this goal by using the TIMER function.
Wow! This is really cool! 8)
-
Is one line being defined by a line ending character? If that's the case, jQuery is a one line library.
-
https://en.wikipedia.org/wiki/Statement_(computer_science)
In computer programming, a statement is the smallest standalone element of an imperative programming language that expresses some action to be carried out. It is an instruction written in a high-level language that commands the computer to perform a specified action.[1] A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g., expressions).
Many languages (e.g. C) make a distinction between statements and definitions, with a statement only containing executable code and a definition instantiating an identifier, while an expression evaluates to a value only. A distinction can also be made between simple and compound statements; the latter may contain
If a function is considered just a definition, then it is a piece of cake to write a one statement program to produce a sin table. But I am pretty sure definitions use variables.
-
:D :D :D
Here it is! no variables, one statement on what SmallBASIC thinks is one line:
? "0 0" + chr(10) + "1 0.8414709848079" + chr(10) + "2 0.90929742682568" + chr(10) + "3 0.14112000805987" + chr(10) + "4 -0.75680249530793" + chr(10) + "5 -0.95892427466314" + chr(10) + "6 -0.27941549819893" + chr(10) + "7 0.65698659871879" + chr(10) + "8 0.98935824662338" + chr(10) + "9 0.41211848524176" + chr(10) + "10 -0.54402111088937" + chr(10) + "11 -0.9999902065507" + chr(10) + "12 -0.53657291800043" + chr(10) + "13 0.42016703682664" + chr(10) + "14 0.99060735569487" + chr(10) + "15 0.65028784015712" + chr(10) + "16 -0.28790331666507" + chr(10) + "17 -0.96139749187956" + chr(10) + "18 -0.75098724677168" + chr(10) + "19 0.14987720966295" + chr(10) + "20 0.91294525072763" + chr(10) + "21 0.83665563853606" + chr(10) + "22 -0.0088513092904" + chr(10) + "23 -0.84622040417517" + chr(10) + "24 -0.90557836200662" + chr(10) + "25 -0.13235175009777" + chr(10) + "26 0.7625584504796" + chr(10) + "27 0.9563759284045" + chr(10) + "28 0.27090578830787" + chr(10) + "29 -0.66363388421297" + chr(10) + "30 -0.98803162409286" + chr(10) + "31 -0.40403764532307" + chr(10) + "32 0.55142668124169" + chr(10) + "33 0.99991186010727" + chr(10) + "34 0.52908268612002" + chr(10) + "35 -0.42818266949615" + chr(10) + "36 -0.99177885344312" + chr(10) + "37 -0.643538133357" + chr(10) + "38 0.29636857870939" + chr(10) + "39 0.96379538628409" + chr(10) + "40 0.74511316047935" + chr(10) + "41 -0.15862266880471" + chr(10) + "42 -0.91652154791563" + chr(10) + "43 -0.8317747426286" + chr(10) + "44 0.01770192510541" + chr(10) + "45 0.85090352453412" + chr(10) + "46 0.90178834764881" + chr(10) + "47 0.12357312274522" + chr(10) + "48 -0.76825466132367" + chr(10) + "49 -0.95375265275947" + chr(10) + "50 -0.26237485370393" + chr(10) + "51 0.67022917584337" + chr(10) + "52 0.98662759204049" + chr(10) + "53 0.39592515018183" + chr(10) + "54 -0.55878904885162" + chr(10) + "55 -0.99975517335862" + chr(10) + "56 -0.52155100208691" + chr(10) + "57 0.43616475524782" + chr(10) + "58 0.99287264808454" + chr(10) + "59 0.63673800713914" + chr(10) + "60 -0.30481062110222" + chr(10) + "61 -0.96611777000839" + chr(10) + "62 -0.73918069664922" + chr(10) + "63 0.16735570030281" + chr(10) + "64 0.92002603819679" + chr(10) + "65 0.8268286794901" + chr(10) + "66 -0.02655115402397" + chr(10) + "67 -0.85551997897532" + chr(10) + "68 -0.89792768068929" + chr(10) + "69 -0.11478481378319" + chr(10) + "70 0.77389068155789" + chr(10) + "71 0.95105465325437" + chr(10) + "72 0.25382336276204" + chr(10) + "73 -0.67677195688731" + chr(10) + "74 -0.98514626046825" + chr(10) + "75 -0.38778163540943" + chr(10) + "76 0.56610763689818" + chr(10) + "77 0.99952015858073" + chr(10) + "78 0.51397845598754" + chr(10) + "79 -0.44411266870751" + chr(10) + "80 -0.99388865392338" + chr(10) + "81 -0.62988799427445" + chr(10) + "82 0.31322878243309" + chr(10) + "83 0.96836446110019" + chr(10) + "84 0.73319032007329" + chr(10) + "85 -0.17607561994859" + chr(10) + "86 -0.92345844700406" + chr(10) + "87 -0.82181783663082" + chr(10) + "88 0.03539830273366" + chr(10) + "89 0.86006940581245" + chr(10) + "90 0.89399666360056" + chr(10) + "91 0.10598751175116" + chr(10) + "92 -0.7794660696158" + chr(10) + "93 -0.94828214126995" + chr(10) + "94 -0.24525198546765" + chr(10) + "95 0.68326171473612" + chr(10) + "96 0.98358774543434" + chr(10) + "97 0.37960773902752" + chr(10) + "98 -0.57338187199042" + chr(10) + "99 -0.99920683418635" + chr(10) + "100 -0.50636564110976"
Here is the program that made the program:
' make sin table program.bas for SmallBASIC 0.12.7 [B+=MGA] 2016-09-23
t = "? "
for i = 0 to 100
if i < 100 then
t = t + enclose(str(i) + " " + sin(i)) + " + chr(10) + "
else
t = t + enclose(str(i) + " " + sin(i))
fi
next
tsave "Sin table program.bas", t
-
Euphoria (no variable name):
include std/math.e
include std/sequence.e
print(1, sin(series(1, 1, 100000)))
I know - the topic is ancient, but I was gone for some time. ;)
-
I know - the topic is ancient, but I was gone for some time. ;)
Well it was addressed to you and I see I used a variable, dang!
But you used 3 statements, 2 for libraries.
Oh wait, I did do it in one statement, no variables, the brute force way! reply #14 not very elegant.
-
Oh wait, I did do it in one statement, no variables, the brute force way! reply #14 not very elegant.
Can you post a code that will calculate and display sin for every number between 1 and 1000000? ;D Here it is in Phix:
print(1, sq_sin(tagset(1000000, 1, 1)))
Pretty elegant, I would say. Quite fast, too. :)
-
Pretty elegant, I would say. Quite fast, too
Here is the Script BASIC version. Not sure what Tomaaz defines as quite fast.
OPEN "sinout" FOR OUTPUT AS #1
FOR s = 1 TO 1000000
PRINT #1,SIN(s),"\n"
NEXT
CLOSE(#1)
jrs@jrs-laptop:~/sb/examples/test$ time scriba sinout.sb
real 0m3.930s
user 0m1.880s
sys 0m1.923s
jrs@jrs-laptop:~/sb/examples/test$ tail -n20 sinout
-0.486438
0.472382
0.996897
0.604869
-0.343273
-0.975811
-0.711193
0.207292
0.935194
0.803283
-0.067163
-0.875859
-0.879294
-0.074310
0.798994
0.937707
0.214296
-0.706138
-0.977352
-0.349994
jrs@jrs-laptop:~/sb/examples/test$
-
John, this topic in not about speed, nor it is about writing a FOR/NEXT loop.
-
Oh, It's about languages that allow multiple statements per line.
-
One statement, no variables... Tomaaz is winning!
Wait, D might create a SpecBas custom built-in... ;D
Phix sq_(func or operator) is interesting. :)
I think Basic's "Core" structure is variables, so ask Phix to do something without using objects.
-
Oh, It's about languages that allow multiple statements per line.
No, it's not. Also, it's not about using one statement. It's about calculating and printing sin for each elements in a range, without using a variable. I don't want to play someone smarter than I am, but possibly we are entering functional programming here, while your code is an example (very obvious and boring) of imperative style.
-
Well this thread, that I started with the title One Statement SIN table, was about One statement, though Tomaaz quote, that I started the thread with, did not mention one statement.
I was going for the elegance of one-liner without : colons for statement separators.
But go ahead and post anything closely related, as you will anyway! ;)
I enjoyed very much Peter's method of using the clock! and learning about other languages by comparing to Basic.
-
One statement, no variables... Tomaaz is winning!
Wait, D might create a SpecBas custom built-in... ;D
I wouldn't hold your breath :)
To be honest, there's really no need to add this sort of functionality to SpecBAS - as Tomaaz points out, we're heading into functional coding territory here and that's not what my BASIC is about - it's a toy language built to scratch my retro-BASIC itch :)
-
Not sure what Tomaaz defines as quite fast.
Jut for curiosity.
OpenEuphoria:
include std/math.e
include std/sequence.e
object plik = open("sinusy.txt", "w")
print(plik, sin(series(1, 1, 1000000)))
close(plik)
tom@helium:~/Downloads$ time eui untitled.ex
real 0m1.103s
user 0m1.044s
sys 0m0.044s
Already, almost 4 times faster that ScriptBasic, but you need to consider that I run it on 5 years old extremely cheap laptop that runs 32-bit OS,
-
My understanding is Open Euphoria is a compiler not an interpreter like Script BASIC.
-
Open Euphoria is a bytecode interpreter and well maybe is fast but not as old original Euphoria[/b].
-
Thanks Aurel for the correction. I just noticed that after visiting the site. I was about to delete my post. :-X
-
Well, that was more about the speed of this example and, in theory, it should be faster than a procedural code doing the same thing, so no surprise that ScriptBasic lost. OpenEuphoria can also translate to C and then compile C code to standalone executables, however the resulting binaries are not much faster than the interpreter, especially on 32-bit Linux distros. All I can get is max 2x. On 64-bit system some examples are 10x faster after compilation. I don't have a Windows machine, so have no idea how it performs under Windows.
-
I can't get my arms around strings being a stream of atoms.
Script BASIC's flexibility and compatibility far outweighs the slight performance difference of other languages.
-
I can't get my arms around strings being a stream of atoms.
It feels a bit strange at the beginning, but it's easy to get used to.
Script BASIC's flexibility and compatibility far outweighs the slight performance difference of other languages.
Compatibility with what? And what is so inflexible about Python, Perl, Lua or OpenEuphoria?
-
Just to be clear. This is not something unique to Euphoria/Phix. You can probably do things like that in any modern language. Python for example:
from math import sin
print(list(map(sin, range(1, 1000000))))
Of course, you can always do it in a procedural style:
from math import sin
for x in range(1000000):
print(sin(x))
So, I don't know what inflexibility John was talking about...
-
So, I don't know what inflexibility John was talking about...
Flexibility dosen't assume everything else is inflexable.
Scripit BASIC's embedding and C extension APIs for a seamless experience is hard to beat.
-
Flexibility dosen't assume everything else is inflexable.
Flexibility that "far outweighs the slight performance difference of other languages" kind of does.
Scripit BASIC's embedding and C extension APIs for a seamless experience is hard to beat.
Some time ago, I heard the same about ScriptBasic arrays. Then it turned out that Perl, Python, Ruby not only had the same flexibility, but also plenty of functions for sorting, mapping, filtering... Does ScriptBasic support unicode out of the box? Regular expressions? Threading? What about support for object oriented or functional programming? "Embedding and C extension APIs" of how many languages have you tried? John, don't get me wrong, but I simply don't believe that ScriptBasic is so good and superior to other languages (at least in some areas) and the fact that virtually no one wants to use it is... well, what? Stupidity of 99.9999% programmers? Conspiracy? Bad luck? Come on...
-
I know what ?
Conspiracy
-
Does ScriptBasic support unicode out of the box? Regular expressions? Threading? What about support for object oriented or functional programming?
Unucode out of the box? - Nope (supports UTF-8)
Regular Expressions? - SB has LIKE/JOKER (high level RE) and a RegEx extension module based on the Peal derivative.
Threading? - SB was designed from the ground up to be thread safe. Check out the SBT extension I wrote. The SB webserver has always been multi-threaded and runs as a process.
OOP? - Check out the VB6 OCX form object I'm interfacing with via COM/OLE automation.
Have you tried Script BASIC or are you like Aurel and just bitch about things you haven't tried?
-
Have you tried Script BASIC or are you like Aurel and just bitch about things you haven't tried?
Yes, I have. And I didn't like it. That's exactly why I "bitch" about it. You will never hear me "bitching" about Haskell, Swift, Rust, C#, Perl 6, Node.js, Ruby On Rails, Django (and many other languages and frameworks), because I've never tried them.
-
Yes, I have. And I didn't like it. That's exactly why I "bitch" about it.
What didn't you like about it that deserves your criticism?
My take is you don't like BASIC in general.
-
What didn't you like about it that deserves your criticism?
It was not easy to set up and use on Linux. There was a list of things "not implemented yet" that wasn't getting smaller. Many features were missing. It felt dated.
My take is you don't like BASIC in general.
Well, after I learnt other languages my interest in BASIC decreased, but I still like BaCon.
John, I know that you're doing great job with ScriptBasic. It's just that sometimes you can't accept the fact that other languages are better (at least in some areas). And that people may prefer slightly different syntax and approach to programming. With all the extensions, ScriptBasic is one of the best BASICs around, but for some people being even the best BASIC is still not enough.
-
Script BASIC is a tool not a solution for all programming tasks. It does what it was designed for well and allows unlimited expansion. That's enough for me to keep using it and advocating for the project.
My main fous is to keep things consistant no matter what OS you're running under. That is why there are OS specific extensions modules.
-
I really wish it was more popular, but to make it happen (with all these languages around) is a hard task and, let's be honest, it's not gonna get easier.
-
I need to get Script BASIC included in a major Linux distribution ASAP.
I think SB would do well in the Rasberry Pi market.
-
:o Talk about degenerating threads! sheez
-
OK so I build my own Range function:
_TITLE "test range.bi and bm"
'$include: 'range.bi'
PRINT Range("1 to 100 step 1 do sin")
PRINT "End of sin(1 to 100 stepping by 1) table."
PRINT: PRINT "Sin at 90 degree intervals:"
PRINT Range("0 to 6.2832 step 1.5708 do sin")
PRINT: PRINT "Squares of numbers from 3.5 to 5.5 stepping by .5"
PRINT Range("5.5 to 3.5 step .5 do square")
'$include: 'range.bm'
Here is a test run of it: