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!

8 comments:

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

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

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

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

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

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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.

    ReplyDelete