Tuesday, April 29, 2008

Haskell Example of ErrorT STArray ST

It counts whether a given word is spellable using the letters one has (and a certain number of blanks). It throws an exception, eventually converted into nothing, if it fails, and returns the remaining tiles if it succeeds.

{- compile with "ghc -fglasgow-exts -package mtl example.hs" -} import Data.Array.MArray(freeze, thaw, writeArray, readArray); import Control.Monad.Trans(lift); import Data.Array(Array); import Data.STRef(readSTRef, newSTRef, STRef, writeSTRef); import Data.Array.ST(STArray); import Control.Monad.Error(runErrorT, throwError, ErrorT, Error(noMsg)); import Control.Monad.ST(runST, ST); type Underflow = (); instance Error Underflow where { noMsg = () } ; process_letter :: STRef s Int -> STArray s Char Int -> Char -> ErrorT Underflow (ST s )(()); process_letter pnum_blanks letters c = (do{ num_that_letter :: Int <- (lift (readArray letters c)); (case num_that_letter of { (0)-> (do{ x_num_blanks :: Int <- (lift (readSTRef pnum_blanks)); (case x_num_blanks of { (0)-> (throwError ()); (num_blanks)-> (lift((writeSTRef pnum_blanks)(pred(num_blanks)))) }); }); (_)-> (lift((writeArray letters c)(pred(num_that_letter)))) }); }); process_word :: Int -> Array Char Int -> String -> Maybe((Int, Array Char Int )); process_word num_blanks letters w = (case (runST(runErrorT((do{ pnum_blanks :: STRef s Int <- (lift (newSTRef num_blanks)); pletters :: STArray s Char Int <- (lift (thaw letters)); (mapM_ (process_letter pnum_blanks pletters) w); blanks_left :: Int <- (lift (readSTRef pnum_blanks)); letters_left :: Array Char Int <- (lift (freeze pletters)); (return (blanks_left, letters_left)); })))) of { (Left(_))-> Nothing; (Right(x))-> (Just x) }); main = undefined

process_word 0 (accumArray undefined 0 ('a','z') []) (repeat 'x') shows it works correctly on infinite input, stopping as soon as it reaches the underflow condition.

Monday, April 28, 2008

Integer triangle

Find triangles whose vertices are all on (square) lattice points, which is not a right triangle, whose edges are all integer length, no edges are orthogonal.

On a triangular lattice? (Not a 60-degree or 120-degree angle.)

Poker on TV

TV coverage needs to show each player's chip stack size and the relative size of the pot with respect to the stack size, (this can be done with simple bars, or with ratios or percentages). In addition to showing the objective probability of winning given knowledge of all the hole cards (this is already done), it should also show each player's own estimate of winning based on lack of knowledge of the other players' hole cards.

Wednesday, April 23, 2008

Sun satellites

A satellite whose elliptical orbit around the Sun periodically brings it close to the Sun where it can effectively collect and store large amounts of solar energy (giant capacitors?) and periodically brings it close to Earth to deposit the energy for Earthlings to use. A constellation of such satellites.

GPS

A discounted GPS device that preferentially computes routes that pass by sponsors' stores.

Sunday, April 20, 2008

Original spline

Consider a spline defined by an origin and an ordered set of points around the origin. The ray from the origin to each point is normal to the spline at that point. This is a simplification of a general spline where the normals may point in arbitrary directions.

Saturday, April 19, 2008

Parallel make

Parallel make (make -j) is a fine language for parallel programming, in fact, for certain problems, better than anything else out there, including "real" parallel programming languages.

Here are some additional features I would like to see: the ability to specify mutexes, that is, that two rules may not be executing simultaneously. Or other limited resources. The ability to specify priorities between two rules if both are able to start. This may be specified by the programmer, or automatically discovered via static or dynamic analysis to maximize throughput. The ability to do encapsulation and scoping i.e., namespace control. A better syntax (than define - call - eval) for parameterized rules. The ability to dynamically create new rules.

Software Bluespec.

Thursday, April 03, 2008

Pythagorean 30

Do nontrivial line segments of integer length exist on the hexagonal or triangular lattice?