hn-classics/_stories/2009/8214927.md

300 lines
8.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
created_at: '2014-08-23T03:45:38.000Z'
title: BASIC as a Haskell DSL (2009)
url: http://augustss.blogspot.com/search/label/BASIC
author: tel
points: 58
story_text: ''
comment_text:
num_comments: 12
story_id:
story_title:
story_url:
parent_id:
created_at_i: 1408765538
_tags:
- story
- author_tel
- story_8214927
objectID: '8214927'
year: 2009
---
[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:
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][1], [Benchmark][7], [DSL][2], [Haskell][3], [LLVM][8]
_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]
## Archives
* [April 2007][26]
* [May 2007][27]
* [June 2007][28]
* [July 2007][29]
* [August 2007][30]
* [October 2007][31]
* [November 2007][32]
* [March 2008][33]
* [July 2008][34]
* [December 2008][35]
* [January 2009][36]
* [February 2009][37]
* [June 2009][38]
* [April 2011][39]
* [May 2011][40]
* [July 2011][41]
* [April 2014][42]
* [December 2014][43]
![Powered by Blogger][44]
* * *
 
[1]: http://augustss.blogspot.com/search/label/BASIC
[2]: http://augustss.blogspot.com/search/label/DSL
[3]: http://augustss.blogspot.com/search/label/Haskell
[4]: http://augustss.blogspot.com/2009/02/more-basic-not-that-anybody-should-care.html "permanent link"
[5]: https://www.blogger.com/comment.g?blogID=25541020&postID=3941171818143196666
[6]: https://img2.blogblog.com/img/icon18_edit_allbkg.gif
[7]: http://augustss.blogspot.com/search/label/Benchmark
[8]: http://augustss.blogspot.com/search/label/LLVM
[9]: http://augustss.blogspot.com/2009/02/is-haskell-fast-lets-do-simple.html "permanent link"
[10]: https://www.blogger.com/comment.g?blogID=25541020&postID=5253528735726768410
[11]: http://augustss.blogspot.com/2009/02/regression-they-say-that-as-you-get.html "permanent link"
[12]: https://www.blogger.com/comment.g?blogID=25541020&postID=7024343775777574640
[13]: https://www.blogger.com/profile/07327620522294658036
[14]: http://news.google.com/
[15]: http://jes6ica.blogspot.com/
[16]: http://augustss.blogspot.com/2014/12/its-time-for-some-more-haskell-opinions.html
[17]: http://augustss.blogspot.com/2014/12/a-commentary-on-24-days-of-ghc_19.html
[18]: http://augustss.blogspot.com/2014/12/a-commentary-on-24-days-of-ghc.html
[19]: http://augustss.blogspot.com/2014/04/haskell-error-reporting-with-locations_5.html
[20]: http://augustss.blogspot.com/2014/04/haskell-error-reporting-with-locations.html
[21]: http://augustss.blogspot.com/2014/04/a-small-haskell-extension.html
[22]: http://augustss.blogspot.com/2011/07/impredicative-polymorphism-use-case-in.html
[23]: http://augustss.blogspot.com/2011/05/more-points-for-lazy-evaluation-in.html
[24]: http://augustss.blogspot.com/2011/04/ugly-memoization-heres-problem-that-i.html
[25]: http://augustss.blogspot.com/2011/04/phew-cleaned-out-lot-of-spam-comments.html
[26]: http://augustss.blogspot.com/2007/04/
[27]: http://augustss.blogspot.com/2007/05/
[28]: http://augustss.blogspot.com/2007/06/
[29]: http://augustss.blogspot.com/2007/07/
[30]: http://augustss.blogspot.com/2007/08/
[31]: http://augustss.blogspot.com/2007/10/
[32]: http://augustss.blogspot.com/2007/11/
[33]: http://augustss.blogspot.com/2008/03/
[34]: http://augustss.blogspot.com/2008/07/
[35]: http://augustss.blogspot.com/2008/12/
[36]: http://augustss.blogspot.com/2009/01/
[37]: http://augustss.blogspot.com/2009/02/
[38]: http://augustss.blogspot.com/2009/06/
[39]: http://augustss.blogspot.com/2011/04/
[40]: http://augustss.blogspot.com/2011/05/
[41]: http://augustss.blogspot.com/2011/07/
[42]: http://augustss.blogspot.com/2014/04/
[43]: http://augustss.blogspot.com/2014/12/
[44]: http://buttons.blogger.com/bloggerbutton1.gif