Saturday, February 07, 2009

More BASIC

Not that anybody should care, but I've reimplemented by BASIC.

Here's a simple program.

{-# LANGUAGE ExtendedDefaultRules, OverloadedStrings #-}
import BASIC

main = runBASIC $ do
    10 GOSUB 1000
    20 PRINT "* Welcome to HiLo *"
    30 GOSUB 1000

    100 LET I := INT(100 * RND(0))
    200 PRINT "Guess my number:"
    210 INPUT X
    220 LET S := SGN(I-X)
    230 IF S <> 0 THEN 300

    240 FOR X := 1 TO 5
    250   PRINT X*X;" You won!"
    260 NEXT X
    270 STOP

    300 IF S <> 1 THEN 400
    310 PRINT "Your guess ";X;" is too low."
    320 GOTO 200

    400 PRINT "Your guess ";X;" is too high."
    410 GOTO 200

    1000 PRINT "*******************"
    1010 RETURN

    9999 END
In some ways this is a step backwards, since it requires some language extensions in Main. But I wanted to be able to use semicolon in the print statement.

But there it is, an exciting game!

*******************
* Welcome to HiLo *
*******************
Guess my number:
50
Your guess 50 is too high.
Guess my number:
25
Your guess 25 is too low.
Guess my number:
37
Your guess 37 is too low.
Guess my number:
44
Your guess 44 is too low.
Guess my number:
47
Your guess 47 is too low.
Guess my number:
48
1 You won!
4 You won!
9 You won!
16 You won!
25 You won!

Labels: , ,

8 Comments:

Blogger Josef said...

So you're saying that without the semicolons your BASIC embedding is Haskell98?

Saturday, February 7, 2009 at 11:32:00 PM GMT  
Blogger augustss said...

Josef: Well, no. But without the semicolon thing the main module doesn't need any extensions. That's not true for the BASIC module.

Sunday, February 8, 2009 at 1:24:00 AM GMT  
Blogger meteficha said...

Could you please remove 'sranddev' from BASIC so we can use it on non-BSDs? =D

Saturday, February 21, 2009 at 3:40:00 PM GMT  
Blogger meteficha said...

Also, line 62 on Parser.hs seems a typo ("<-").

Saturday, February 21, 2009 at 5:22:00 PM GMT  
Blogger zsbana said...

Would it be possible to name the equality operator and the assignment statement the same, just like they are in real Basic?

Wednesday, February 25, 2009 at 10:54:00 AM GMT  
Blogger Jace said...

Hmm, something odd there.

BASIC didn't use := for assignment. That's Pascal. BASIC used = for both assignment and comparison, switching to the latter for IF/WHILE/etc statements.

Thursday, April 23, 2009 at 5:00:00 AM GMT+1  
Blogger Orion Blastar said...

You are using the variable X twice. Once to hold the guess of the user and then a second time to do a loop after the user guesses the right number. This is very sloppy code. You should use Z or something else for the loop variable.

Saturday, August 23, 2014 at 6:51:00 PM GMT+1  
Blogger augustss said...

With only 26 variables, recycling is a necessity. :)

Sunday, August 24, 2014 at 2:31:00 AM GMT+1  

Post a Comment

<< Home