login
Header Space

 
 

Greg Buchholz's blog

Non-ugly average in Haskell

November 8, 2008 - 12:01am
Submitted by Greg Buchholz on November 8, 2008 - 12:01am.

data E a b = L (E a b) | R b deriving (Show)

fold f acc [] = R acc                      
fold f acc (x:xs) = let x' = f acc x 
                    in x' `seq` (L $ fold f x' xs)

lift2 f = \x y -> par_eval x y
    where par_eval   (L x)   (L y) = par_eval x y
          par_eval x@(R _)   (L y) = par_eval x y
          par_eval   (L x) y@(R _) = par_eval x y
          par_eval   (R x)   (R y) = R $ f x y

lift1 f (L x) = lift1 f x
lift1 f (R x) = R (f x)

sum' xs = fold (+) 0 xs
len' xs = fold (\x y->succ x) 0 xs
avg  xs =  (sum' xs) / (len' xs)

main = print $ avg [1..1e7]

instance Eq b => Eq (E a b) where
    a == b = case (lift2 (==) a b) of (R x) -> x

instance Num b => Num (E a b) where
   (+) = lift2 (+)
   (*) = lift2 (*)
   fromInteger = R . fromInteger
   abs = lift1 abs 
   signum = lift1 signum

instance Fractional b => Fractional (E a b) where
   (/) = lift2 (/)
   fromRational = R . fromRational

Entering unicode

November 23, 2007 - 8:57pm
Submitted by Greg Buchholz on November 23, 2007 - 8:57pm.

Here's the configuration file I use to help me enter some of the fun mathematical unicode characters with vim. The commands were borrowed from TeX...

imap jj <ESC>
imap \div ÷
imap \times ×
imap \sum ∑
imap \int ∫
imap \oint ∮
imap \angle ∠
imap \forall ∀
imap \exists ∃
imap \partial ∂
imap \prod ∏
imap \infty ∞
imap \pm ±
imap \sqrt √
imap \circ ∘
imap \Re ℛ
imap \Im ℑ

Misc. ray tracers

October 10, 2007 - 5:01pm
Submitted by Greg Buchholz on October 10, 2007 - 5:01pm.


By popular demand, here's the more web accessible versions of a couple of simple ray tracers that I quickly cranked out in Perl and Haskell. Along with the respective source code.

It takes a POV-like scene file, and spits out a *.ppm file onto STDOUT.

Cellular Automata

March 8, 2007 - 6:15pm
Submitted by Greg Buchholz on March 8, 2007 - 6:15pm.

After seeing a cellular automata simulator in F#, I thought I'd make a more general version with Perl/TK which was also shorter. Invoke with a numeric argument on the command line to see automata other than rule 30 (e.g. 90, 110).

SWI-Prolog and Unicode.

February 15, 2007 - 2:17pm
Submitted by Greg Buchholz on February 15, 2007 - 2:17pm.

% fun with SWI-Prolog and Unicode.  Try some queries like... 
% Ans ≔ ∫x d x.
% Ans ≔ ∫ 2 + x^3 d x.

:- encoding(utf8).
:- op(699, xfy,).
:- op(600, fx,).
:- op(510, xfx, d).
:- op(200, xf, ²).

A+B ≔ ∫ Y+Z d X :- A ≔ ∫ Y d X, B ≔ ∫ Z d X. 
C*A ≔ ∫ C*Y d X :- free(C,X), A ≔ ∫ Y d X.
C*X ≔ ∫ C d X   :- free(C,X).
X² / 2 ≔ ∫ X d X.
X^N1/N1 ≔ ∫ X^N d X :- free(N,X), N =\= -1, N1 is N + 1. 
(log(A*X+B)/A) ≔ ∫ 1/(A * X + B) d X :- free(A,X),free(B,X).
(exp(A*X+B)/A) ≔ ∫ exp(A*X+B) d X    :- free(A,X), free(B,X).E d X ≔ ∫ E d X.

free(C,X) :- \+ bound(C,X).

bound(X,X).
bound(A+B,X) :- bound(A,X); bound(B,X).
bound(A-B,X) :- bound(A,X); bound(B,X).
bound(A*B,X) :- bound(A,X); bound(B,X).
bound(A/B,X) :- bound(A,X); bound(B,X).
bound(A^B,X) :- bound(A,X); bound(B,X).

Breaking free

January 4, 2007 - 11:27pm
Submitted by Greg Buchholz on January 4, 2007 - 11:27pm.

If you really want to break free from the von Neumann bottleneck , grab yourself an FPGA starter kit and start playing with Verilog or VHDL. FPGAs are quite interesting now that features like hardware multipliers are becoming more widespread.

Maybe monad in C++

December 18, 2006 - 12:26pm
Submitted by Greg Buchholz on December 18, 2006 - 12:26pm.

C++ is a complicated language, but it doesn't have to be that complicated. Here a simple maybe monad implemented in C++...

Planet "Programming Language Dilettante"

December 14, 2006 - 7:12pm
Submitted by Greg Buchholz on December 14, 2006 - 7:12pm.

Planet "Programming Language Dilettante". That's one one blog aggregator I'd really like to read.

The more things change the more they stay the same...

December 14, 2006 - 1:25am
Submitted by Greg Buchholz on December 14, 2006 - 1:25am.

After we correct for the algorithmic differences, is there really that much difference between...

fibo 0 = 1 
fibo 1 = 1 
fibo x = fibo (x-1) + fibo (x-2)

Blub is the language you know.

November 20, 2006 - 3:02pm
Submitted by Greg Buchholz on November 20, 2006 - 3:02pm.


Pascal Costanza wrote:


But in the general case, dynamic typing and static typing are not compatible. In Common Lisp, there are a lot of cases where you can very conveniently change types at runtime. So a static check of types would be a great burden. Here is my standard example for this:

(defclass person () 
    ((name :initarg :name))) 
 
(let ((p (make-instance 'person :name "Pascal"))) 
    (eval (read)) 
    (setf (slot-value p 'address) "Brussels")) 


There is no chance to see up front whether this program will fail or not, since the user can redefine the class person when (eval (read)) is executed. At that time, the slot 'address could be added to the class person.

I don't see any reason why that's not amenable to static typing...

Memoizing fixed point combinator in C++

November 14, 2006 - 1:41pm
Submitted by Greg Buchholz on November 14, 2006 - 1:41pm.

Over on comp.lang.c++ they're discussing memoization of functions. Here's my version of a memoized fix point combinator...

#include<iostream>
#include<map>

std::map<int,int> memo_table;

struct Fix
{
    int (*f)(int,Fix);

    int operator()(int x, Fix g)
    { 
        return (memo_table.find(x) != memo_table.end()) 
               ? memo_table[x]
               : memo_table[x] = g.f(x,g);
    };
};

int fib(int n, Fix f) { return n < 2 ? n : f(n-1,f) + f(n-2,f); }

int main(int argc, char *argv[])
{
    Fix g = {fib};
    std::cout << g(40,g) << std::endl;
    return 0;
}

Type system computation

November 8, 2006 - 3:03pm
Submitted by Greg Buchholz on November 8, 2006 - 3:03pm.

Over on comp.lang.lisp we have a comparison of between the type systems of lisp and ML. I don't know enough about ML to know whether it is or isn't up to the challenge, but here's a teaser of how you could go about it in Haskell. First off, we need a data type that knows about the range of possible values for its integral argument. And then we need to be able to exponentiate the range...

Compiling Omega with GHC 6.6

October 24, 2006 - 10:59am
Submitted by Greg Buchholz on October 24, 2006 - 10:59am.

Here are the changes I needed to make in order to get Omega to compile with ghc-6.6.

Changlog entry:

18Oct2006  Greg Buchholz 

  * DepthFirstSearch.hs : Changed from using the ST module to Control.Monad.ST
                          and Data.Array.ST
  * Infer2.hs : Modified to use Data.Map instead of deprecated Date.FiniteMap
  * Toplevel.hs : Modified to use Data.Map 
  * Makefile : Get rid of ghc-6.4 specific "-package lang", let "-make"
               figure it out for us.

...and the diffs.

Arbitrage Opportunity

September 26, 2006 - 4:12pm
Submitted by Greg Buchholz on September 26, 2006 - 4:12pm.

Looks like there's an arbitrage opportunity in books over in the Amazon realm. Let's take a book like Types and Programming languages. At Amazon.com, the price is $64.60(USD), while over at Amazon.ca, the price is $51.74 (CDN) (or about $46.57 USD). And the book Structure and Interpretation of Classical Mechanics is $57.40 in the U.S.A. while again $46.57 USD in Canada. And the situation is reversed for the Minix book: $82.40 in the U.S. and $117.85 USD in Canada. What's really strange to me though, is that there are price differences in both directions. I could understand if for some reason, prices were generally higher in one country versus another (import duties/high real estate prices/etc.).

Of course, you can't easily take advantage of this because Amazon wants to charge you an $8 surcharge for international orders, plus another additional $2 for each book. Seems like someone living just across the border (maybe in Montana, with no sales tax, or Alberta, with no PST) could set up a business to make some cash by buying books from Amazon, to sell on Amazon's new/used book marketplace (of course, shipping costs would eat into the profits, and if you really were reselling on Amazon, you'd have to pay their commission.) Anyway, you'll know that the global economy has arrived when this oddball price discrimination becomes unsustainable.

gossip

June 20, 2006 - 8:09pm
Submitted by Greg Buchholz on June 20, 2006 - 8:09pm.

"I spend my free time reading web sites..."

Fortunately, there is a salve to soothe that particular problem...

For my part, I could easily do without the post-office. I think that there are very few important communications made through it. To speak critically, I never received more than one or two letters in my life -- I wrote this some years ago -- that were worth the postage. The penny-post is, commonly, an institution through which you seriously offer a man that penny for his thoughts which is so often safely offered in jest. And I am sure that I never read any memorable news in a newspaper. If we read of one man robbed, or murdered, or killed by accident, or one house burned, or one vessel wrecked, or one steamboat blown up, or one cow run over on the Western Railroad, or one mad dog killed, or one lot of grasshoppers in the winter -- we never need read of another. One is enough. If you are acquainted with the principle, what do you care for a myriad instances and applications? To a philosopher all news, as it is called, is gossip, and they who edit and read it are old women over their tea.

speck-geostationary