8.1 KiB
created_at | title | url | author | points | story_text | comment_text | num_comments | story_id | story_title | story_url | parent_id | created_at_i | _tags | objectID | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2014-08-23T03:45:38.000Z | BASIC as a Haskell DSL (2009) | http://augustss.blogspot.com/search/label/BASIC | tel | 58 | 12 | 1408765538 |
|
8214927 |
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!
posted by augustss at 8:31 PM 8 comments
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:
LBB1_1: ## _L4
movsd LCPI1_0, %xmm2
movapd %xmm1, %xmm3
addsd %xmm2, %xmm3
ucomisd LCPI1_1, %xmm3
divsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movapd %xmm3, %xmm1
jne LBB1_1 ## _L4
Labels: BASIC, Benchmark, DSL, Haskell, LLVM
posted by augustss at 4:50 PM 11 comments
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.)
posted by augustss at 11:12 AM 13 comments
About Me
- : Name: augustss
-
Location: London, United Kingdom
Links
Previous Posts
- A commentary on 24 days of GHC extensions, part 3
- A commentary on 24 days of GHC extensions, part 2
- A commentary on 24 days of GHC extensions
- Haskell error reporting with locations, update
- Haskell error reporting with locations
- A small Haskell extension
- Impredicative polymorphism, a use case In a recen...
- More points for lazy evaluation In a recent blo...
- Ugly memoization Here's a problem that I recentl...
- Phew! Cleaned out a lot of spam comments in my bl...
Archives
- April 2007
- May 2007
- June 2007
- July 2007
- August 2007
- October 2007
- November 2007
- March 2008
- July 2008
- December 2008
- January 2009
- February 2009
- June 2009
- April 2011
- May 2011
- July 2011
- April 2014
- December 2014