[Source](http://augustss.blogspot.com/search/label/BASIC "Permalink to Things that amuse me")
# Things that amuse me
# Things that amuse me
## 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: [BASIC][1], [DSL][2], [Haskell][3]
_posted by augustss at [8:31 PM][4]_ [8 comments][5] ![][6]
## Friday, February 06, 2009
## Is Haskell fast?
Let's do a simple benchmark comparing Haskell to C. My benchmark computes an approximation to infinity by adding up 1/n. Here is the C code:
#include<stdio.h>
int
main(int argc, char **argv)
{
double i, s;
s = 0;
for (i = 1; i <100000000;i++)
s += 1/i;
printf("Almost infinity is %gn", s);
}
And running it
Lennarts-Computer% gcc -O3 inf.c -o inf
Lennarts-Computer% time ./inf
Almost infinity is 18.9979
1.585u 0.009s 0:01.62 97.5% 0+0k 0+0io 0pf+0w
And now the Haskell code:
import BASIC
main = runBASIC' $ do
10 LET I =: 1
20 LET S =: 0
30 LET S =: S + 1/I
40 LET I =: I + 1
50 IF I <> 100000000 THEN 30
60 PRINT "Almost infinity is"
70 PRINT S
80 END
And running it:
Lennarts-Computer% ghc --make Main.hs
[4 of 4] Compiling Main ( Main.hs, Main.o )
Linking Main ...
Lennarts-Computer% ./Main
Almost infinity is
18.9979
CPU time: 1.57s
As you can see it's about the same time. In fact the assembly code for the loops look pretty much the same. Here's the Haskell one:
_posted by augustss at [4:50 PM][9]_ [11 comments][10] ![][6]
## Regression
They say that as you get older you regress back towards childhood. So I present you with today's Haskell program (the idea shamelessly stolen from JoshTriplett from #haskell on IRC):
import BASIC
main = runBASIC $ do
10 LET X =: 1
20 PRINT "Hello BASIC world!"
30 LET X =: X + 1
40 IF X <> 11 THEN 20
50 END
Yes, it runs. (I'm sorry about the `=:` instead of `=`, but some things are just too wired into Haskell to change.)
Labels: [BASIC][1], [DSL][2], [Haskell][3]
_posted by augustss at [11:12 AM][11]_ [13 comments][12] ![][6]
## About Me
: **Name:** [ augustss ][13]
: **Location:** London, United Kingdom
[View my complete profile][13]
## Links
* [Google News][14]
* [Jessica][15]
## Previous Posts
* [A commentary on 24 days of GHC extensions, part 3][16]
* [A commentary on 24 days of GHC extensions, part 2][17]
* [A commentary on 24 days of GHC extensions][18]
* [Haskell error reporting with locations, update][19]
* [Haskell error reporting with locations][20]
* [A small Haskell extension][21]
* [ Impredicative polymorphism, a use case In a recen...][22]
* [ More points for lazy evaluation In a recent blo...][23]
* [ Ugly memoization Here's a problem that I recentl...][24]
* [Phew! Cleaned out a lot of spam comments in my bl...][25]