News for hackers. Stuff that matters.
Re:A Universe in One Line of Code with 10 PRINT by japhyr on Sunday November 12, 2017 @01:25PM (#15681887)
This has me reflecting on how I learned to program, compared to how people learn to program today.

I learned to program using BASIC when I was between 5 and 10 years old. My dad was a software engineer at DEC, and he had a kit computer in our basement that he let us play with once in a while. My first program was a number-guessing game, and it went something like this:

    10  NUMBER = 23
    20  PRINT "I'm thinking of a number."
    30  INPUT "Make a guess: " $GUESS
    40  IF GUESS < NUMBER THEN
    50    PRINT "Too low!"
    60    GOTO 30
    70  ELSEIF GUESS > NUMBER THEN
    80    PRINT "Too high!"
    90    GOTO 30
    100 PRINT "You got it!"
That was pretty straightforward, because everything about it was linear except for the GOTOs. You can think about each step with very little planning, and when you need a break in program flow you can use a GOTO. It also means when teaching, you can introduce each new concept as the need arises: store a value, print a statement, prompt for input, check a condition, and go back to a previous statement.

Let's see the same program in Python:

    number = 23
    print("I'm thinking of a number.")

    guess = 0
    while guess != number:
        guess = input("Make a guess: ")
        guess = int(guess)
        if guess < number:
            print("Too low!")
        elif guess > number:
            print("Too high!")
        else:
            print("You got it!")
The overall program logic isn't much different when you read the code. But to write this code, you need to know a number of things in advance. You need to know how you're going to approach the problem. You need to know what a loop is, and specifically what a while loop is. Conceptually, especially for a young person, a while loop is much more abstract than a GOTO statement.

I am happy that people get to learn a language like Python as their first language today. They can do much more interesting things, in a shorter amount of time, than they could in basic 40 years ago. But the very first programs people write seem fundamentally different, and that's not a bad thing.

Re:A Universe in One Line of Code with 10 PRINT by yoz-y on Sunday November 12, 2017 @02:29PM (#15682179)
Very well put. I remember writing gotos everywhere when I learnt to code. Interestingly enough my first program was almost the same as yours. Great thing about a goto is that it is a single command which can emulate any of the other constructs.

Also one thing I remember from back when was that if there was a problem I did not understand (I was mostly learning by modifying example programs that I somehow got) the only way to solve them was to try more things and harder. I did not have any books and internet was a luxury. Eventually I have understood more of the code and got better.

Nowadays it is way easier to remain ignorant and still be productive.

Re:A Universe in One Line of Code with 10 PRINT by laumars on Sunday November 12, 2017 @06:16PM (#15683093)
> Great thing about a goto is that it is a single command which can emulate any of the other constructs.

The thing is BASIC does have other constructs. GOSUB[0] has been around in the 70s and that worked like a GOTO except when a RETURN statement was hit the code would then jump back to the point where it GOSUB'ed - effectively behaving as a rudimentary subroutine (hence the name, "GoSub").

There was also the FN[2][3] statement that allowed you to write basic functions, albeit you were limited to 1 liners. But by the 80s some dialects of BASIC were supporting more complex procedures[3][4].

[0] http://www.hoist-point.com/applesoft_basic_tutorial.htm#stri...

[1] http://www.cpcwiki.eu/index.php/Locomotive_BASIC#FN

[2] https://www.c64-wiki.com/wiki/FN

[3] http://www.bbcbasic.co.uk/bbcwin/tutorial/chapter16.html

[4] http://www.bbcbasic.co.uk/bbcwin/tutorial/chapter17.html

Re:A Universe in One Line of Code with 10 PRINT by int_19h on Sunday November 12, 2017 @09:13PM (#15683765)
Also, didn't the FOR..NEXT loop show up really early on?
Re:A Universe in One Line of Code with 10 PRINT by laumars on Monday November 13, 2017 @02:28AM (#15684864)
Yup. Also WHILE/WEND loops and some basic error control with ERROR (raise), ON ERROR (capture).

BASIC certainly had it's faults but it was a reasonably complete language. However it can't have helped the number of different dialects there were in the 70s and 80s. Each brand of consumer electronics seemed to ship with a subtly different list of statements which meant I think lead people to often stick with the core basics of, erm, BASIC.

Re:A Universe in One Line of Code with 10 PRINT by int_19h on Monday November 13, 2017 @04:16AM (#15685272)
Pretty sure WHILE/WEND was a slightly later addition.

I just went and looked up Dartmouth BASIC (this is the original thing). The list of keywords shows FOR, but not WHILE. This is the manual for v2, as early as I could find (1964) - it also has FOR already, but not WHILE:

http://www.bitsavers.org/pdf/dartmouth/BASIC_Oct64.pdf

(Side note: it's kinda amusing that the document uses a slash-through to distinguish the letter O from zero, except it does it in a way that is reverse of what is common today: it's the letter that has a slash through it!)

I do wonder if it was a true loop, though, or whether it was implemented more along the lines of GOSUB/RETURN (i.e. whether FOR basically set up a dynamic label, and NEXT did an indirect jump). If so, then it should be possible to have two FOR statements with the same variable, both "matched" to a single NEXT, if GOTO is used to reach it from one of the loops.

Re:A Universe in One Line of Code with 10 PRINT by laumars on Monday November 13, 2017 @04:41AM (#15685359)
Ahh sorry, I was talking in the context of the microcomputer era of the late 70s and 80s rather than the full history of BASIC.

Thanks for the PDF, that will make really interesting lunch time reading :)

> I do wonder if it was a true loop, though, or whether it was implemented more along the lines of GOSUB/RETURN (i.e. whether FOR basically set up a dynamic label, and NEXT did an indirect jump). If so, then it should be possible to have two FOR statements with the same variable, both "matched" to a single NEXT, if GOTO is used to reach it from one of the loops.

I could be wrong here but I think most high level structures (switch, for loop, etc) end up getting translated to GOTOs at the machine level anyway. So you might be right with regards to the BASIC interpreter.

As for whether you could have fun with that, Dartmouth is before my time but I remember Locomotive BASIC (Amstrad CPC 464, so we're talking early 80s) used to do run time checks to ensure you weren't abusing NEXT inside nested FORs.

Re:A Universe in One Line of Code with 10 PRINT by int_19h on Monday November 13, 2017 @07:05PM (#15691426)
I tried it on QBASIC and Turbo BASIC, and both require matching FOR and NEXT. Now I'm really tempted to set up a Dartmouth emulator to see if it was also true there...
Re:A Universe in One Line of Code with 10 PRINT by cicero on Sunday November 12, 2017 @10:36PM (#15684104)
It was in Integer BASIC and Applesoft BASIC on the Apple II. You could say something like this to count to 10:

    10 FOR I=1 TO 10
    20 PRINT I
    30 NEXT I
Re:A Universe in One Line of Code with 10 PRINT by laumars on Sunday November 12, 2017 @03:33PM (#15682494)
> I am happy that people get to learn a language like Python as their first language today. They can do much more interesting things, in a shorter amount of time, than they could in basic 40 years ago.

How much is down to the language and how much is that to do with the capability of computers then vs now. A fair few dialects of BASIC could manipulate the hardware directly but the capability of those machines were always quite limited. However with the hacker community going strong for retro hardware, you can now get all sorts of cool mods, like the WiFi addon I have on my Amstrad CPC 464 which allows me to even make HTTP GET requests, from BASIC. Ok I'm still limited by 64K of memory but that just forces you to think more creatively.

I'm not denying that Python teaches better programming principles than the early dialects of BASIC, but again how much of that is down to age given than newer dialects of BASIC can be structured very much like a typical imperative program. ie no requirement for line numbers, proper functions (though some dialects of BASIC have supported this since the 80s), etc.

Sometimes though, I think the simplicity of your environment can count for a lot. I remember reading an interview with a dance DJ turned producer who commented that he regretted buying lots of hardware when he was starting out as it slowed his learning curving (too much tech to learn in one go) and thus he would have been better off buying one synth, mastering that, then moving onto another synth. I wonder if programming can be like that for youngsters these days. I wonder if they get crippled by the vastness of the options out there and all the layers of the stack involved in even the simplest of "Hello world!" programs. Yet BASIC on the micro computers of the 70s and 80s offered a simple, no hassle, interface to that allowed our young imaginations to fly. I think we were very lucky to grow up in that era.

Re:A Universe in One Line of Code with 10 PRINT by keypress on Monday November 13, 2017 @06:40AM (#15685762)
Defo, my Mum's partner had an Atari ST with cubase etc. and a couple of keyboards and he used to have fun and make music. Ever since he bought his first PC, he has made no music whatsoever, he spends his time in the Internet wormhole and optimising his hard drive (no kidding). Very easy to focus on your tool set and not actually use them.
Re:A Universe in One Line of Code with 10 PRINT by Jare on Sunday November 12, 2017 @03:37PM (#15682511)
Hehe the guessing game was my fist too, way before I even had a computer.

I think what we have lost from the old 8-bit computers and their BASIC, was the ability to instantly have something show on the screen, like the classic Sinclair Spectrum spider web:

    10 FOR n=0 TO 31
    20 PLOT n*11,0
    30 DRAW -n*11, 175-n*8
    40 NEXT n
(or something like that) That stuff got me hooked.
Re:A Universe in One Line of Code with 10 PRINT by Turing_Machine on Sunday November 12, 2017 @06:55PM (#15683238)
You can get pretty close to that with JavaScript, especially with all the CodePen-like sites out there -- but even without those, the JavaScript console is pretty similar to the old BASIC prompt in terms of immediacy and ubiquity.

JavaScript also has this in common with BASIC: the language bigots hate it. :-)

Re:A Universe in One Line of Code with 10 PRINT by wruza on Monday November 13, 2017 @03:58AM (#15685216)
I like BASIC, C, D, Objective C, Perl, Lua, 8080 and x86 Assembly (no code for around 15 years, but still) and probably few others catched my eye, like Io, LISP, Haskell, but I had no time to master these, only done a bunch of snippets discovering their power. But I hate js (and php) with all of my soul’s strings, no matter how much time I spent learning or fixing it. How about that single data point.

Otoh, I bet most js bigots only know one language very good, guess what it’s name is.

Re:A Universe in One Line of Code with 10 PRINT by Jare on Monday November 13, 2017 @03:34PM (#15689968)
Indeed! I defended JS for this exact purpose after the 2D canvas became truly ubiquitous. Still a minor amount of boilerplate but you can be drawing stuff in no time. With an integrated editor it would be perfect!
Re:A Universe in One Line of Code with 10 PRINT by Momquist on Sunday November 12, 2017 @10:43PM (#15684133)
Python still has Turtle in its standard library, with which you can create direct and simple graphics like you describe.

https://docs.python.org/3/library/turtle.html

Re:A Universe in One Line of Code with 10 PRINT by tomsmeding on Monday November 13, 2017 @07:05AM (#15685885)
There is also a thing called Logo (http://www.softronix.com/logo.html); that implementation is windows-only and old, but it was a glorious first programming language for me. There is also a unix version called ucblogo, but when I tried that a while ago on mac, it was terribly slow and didn't have a number of the nice additions that mswlogo had.
Re:A Universe in One Line of Code with 10 PRINT by tomsmeding on Saturday November 18, 2017 @05:26AM (#15728486)
For people looking through the archives: FMSLogo is an updated version of MSWLogo, and is linked to from the same page. It runs beautifully under Wine, too, so not being on Windows is no excuse for not trying it out. ;)
Re:A Universe in One Line of Code with 10 PRINT by qubex on Monday November 13, 2017 @02:31PM (#15689398)
Fun fact: LOGO is a LISP.
Re:A Universe in One Line of Code with 10 PRINT by hexane360 on Sunday November 12, 2017 @04:28PM (#15682718)
This is a niche I think Scratch (https://scratch.mit.edu/) does a pretty good job of filling
Re:A Universe in One Line of Code with 10 PRINT by jpalomaki on Monday November 13, 2017 @04:00AM (#15685228)
And if I remember right, on some Spectrum model you even had all the basic commands written on keys (I was once hunting with a friend for the famous "any" key which we had determined had to be pressed in order to get to play Pyjamarama)
Re:A Universe in One Line of Code with 10 PRINT by foo101 on Sunday November 12, 2017 @10:21PM (#15684023)
Your code does not work for me. I get syntax error in line 20: https://imgur.com/a/mqI29. What did I do wrong and how can I get it to work?
Re:A Universe in One Line of Code with 10 PRINT by mrob on Monday November 13, 2017 @04:58AM (#15685406)
It's for the ZX Spectrum: https://imgur.com/a/Yb757

There's a Javascript emulator here: http://jsspeccy.zxdemo.org/

128K mode is easier because it lets you type the keywords instead of pressing single key shortcuts. For the punctuation, type:

ctrl-l for =

ctrl-b for *

ctrl-alt-n for ,

ctrl-j for -

Re:A Universe in One Line of Code with 10 PRINT by steveklabnik on Sunday November 12, 2017 @05:27PM (#15682942)
I learned exactly this way, with GWBASIC, with the same program. It's why I chose this program as the start of "The Rust Programming Language", honestly. Similar to your point about Python, it's more complex. Then again, so are computers...

I'm lucky enough to still have the book for my first computer, my second encounter with BASIC. I tweeted some photos of it last year https://twitter.com/steveklabnik/status/721471207883808769

Re:A Universe in One Line of Code with 10 PRINT by d0mine on Monday November 13, 2017 @07:47AM (#15686073)
By introducing a couple of functions: say() and audio_input(), you could make it into a talking game:

  number = 4
  say("I'm thinking of a number")
  while True:
      try:
          word = audio_input("Make a guess: ").lower()
      except RuntimeError:
          say("Try again")
      else:
          say(word)
          guess = word2num.get(word)
          if guess is None:
              say("Not a number")
          elif guess < number:
              say("Too low!")
          elif guess > number:
              say("Too high!")
          else: 
              say("You got it!")
              break
Complete code example https://gist.github.com/3b3f26602e31198d399c643c8053b8b6
Re:A Universe in One Line of Code with 10 PRINT by int_19h on Sunday November 12, 2017 @09:12PM (#15683761)
I've learned my BASIC with Turbo BASIC on DOS, which already had WHILE/WEND. I don't recall any trouble with this concept.

When I moved on to QBASIC, which had a wonderful DO..LOOP construct which generalized loops with positive and negative pre- or post-conditions, also allowing for no conditions at all, it was really awesome and just felt "right".

I also remember how befuddled I was by GOSUB/RETURN, having seen it after learning DEF FN and SUB. The idea that you could just fall through the "end" of a procedure by not returning from it seemed nuts.

Re:A Universe in One Line of Code with 10 PRINT by underwoodley on Monday November 13, 2017 @08:38AM (#15686424)
I don't see these programs as fundamentally different. The only material different is that one uses a GOTO and the other uses a while loop.

But I don't think either of these is more intuitive. When you learnt how a GOTO works, you probably learnt immediately afterwards how to use it to loop. This loop is a 'pattern', one which you would have to grok as a single idea before you could write a program like the above. This is similar to learning about a while loop, and then knowing you would need one when you wrote the Python script.

Re:A Universe in One Line of Code with 10 PRINT by wruza on Monday November 13, 2017 @03:31AM (#15685102)
I think this snippet is missing either ENDIF at 95 or the fact that

  IF ... THEN <x> ELSE <y> 
accepts either a single statement or a line number to go to as <x> and <y>. So, in dialects where ENDIF doesn’t exist at all, it was actually a bit harder than just writing or thinking WHILE-style. When I moved from ZX spectrum-clone basic to gwbasic and tbasic/qbasic, it felt much easier. (I was also more advanced by the time, so can’t tell how it changed my view on the old style.)

Edit: formatting

Re:A Universe in One Line of Code with 10 PRINT by hnruss on Monday November 13, 2017 @11:09AM (#15687457)
I also learned to program in BASIC, but I learned by creating games on the TI-83+ graphing calculator. The limited nature of the platform led me to learn quite a lot in addition to the basics: performance optimization, garbage collection, matrices, lists, UX, maintainability, keyboard shortcuts, and how to read documentation.

Would I recommend this platform to someone else? Only if they needed to learn to program while pretending to do math homework.

Re:A Universe in One Line of Code with 10 PRINT by Kagerjay on Tuesday November 14, 2017 @12:30AM (#15692905)
My first programming course was in fortran and i got away without using alot of built in loop logic using goto statements.

Now i never use any goto statements ever

Re:A Universe in One Line of Code with 10 PRINT by hammock on Monday November 13, 2017 @10:44AM (#15687227)
I'm pretty sure you can write the program in BASIC without GOTOs. Doesn't BASIC have WHILE loops?