Wednesday, December 31, 2008

Sunday, December 21, 2008

To Read - Haskell Type Goodness


Read http://okmij.org/ftp/Haskell/types.html to gain insight into a host of different ways in which the powerful Haskell type system can be (ab)used to get, for instance -

polyvariadic functions (which take a variable number of arguments with potentially different types)

functions with genuine keyword arguments,

static array bounds checking,

automatically reifying a type to yield a function with the desired properties (using only the haskell typechecker. No external tools/extensions are needed because Haskell already has extensive reflexive properties),

strongly typed heterogenous collections,

and a whole bunch of other stuff that will blow your mind!

The following code causes Haskell 98 to give a Segmentation Fault!!!

module C where
     
     import Data.Typeable
     import Data.Maybe
     
     newtype W a = W{unW :: a}
     instance Typeable (W a) where typeOf _ = typeOf ()
     
     bad_cast x = unW . fromJust . cast $ W x
     -- inferred type: bad_cast :: a -> b
     
     test1 = bad_cast True ++ ""

Sad but true...

Real World Haskell the book - under CC license!

Read it online here - http://book.realworldhaskell.org/read/

Muy muy bien!


Line numbers in emacs in three easy steps


Line numbers in emacs in three easy steps (tested on GNU Emacs 22.3.1 (i386-mingw-nt5.1.2600) for Windows) -

1. Download and place linum.el in your emacs lisp library folder.

2. Then add the following lines to your init file (~/emacs.d/init.el) file -

;;;;Show line numbers
(require 'linum)
;;;;Add a space after each line number
(setq linum-format "%d ")
3. Finally either - Toggle display of line numbers with M-x linum-mode.  OR paste the following in your ~/emacs.d/init.el file -
;;;;Enable display of line numbers for all open files
(global-linum-mode 1)

S W E E T!

Saturday, December 20, 2008

Geek Poem

The text of the poem follows:

<>!*''#
^"`$$-
!*=@$_
%*<>~#4
&[]../
|{,,SYSTEM HALTED

The poem can only be appreciated by reading
it aloud, to wit:

Waka waka bang splat tick tick hash,
Caret quote back-tick dollar dollar dash,
Bang splat equal at dollar under-score,
Percent splat waka waka tilde number four,
Ampersand bracket bracket dot dot slash,
Vertical-bar curly-bracket comma comma
CRASH.


SRC: INFOCUS magazine. Original authors, Fred Bremmer and Steve Kroese of Calvin College & Seminary of Grand
Rapids, MI.

Friday, December 19, 2008

Eggshell with Romalian Type




What do you think? Very nice. Picked them up from the printer’s yesterday. Good coloring. That’s bone. And the lettering is something called Silian Rail. It’s very cool Bateman, but that’s nothing. Look at this. That is really nice. Eggshell with Romalian type. What do you think? Nice. That is really super. I can’t believe that Bryce prefers Van Patten’s card to mine. But wait. You ain’t seen nothin’ yet. Raised lettering, pale nimbus white. Impressive.

Wednesday, December 03, 2008

Bash Script to get maximum directory depth


They used echo sounding to figure out what the deepest part of the ocean was. Too bad the echo command in Linux does something totally different..

I needed a way to figure out the deepest part of a directory structure, using standard Linux commands only (no php/perl/python etc.).

e.g. if the directory structure is as follows -

d1
->d2
->->d3
->->->d4
->d5
->->d6


I wanted a shell script which will give me the answer "4" (i.e. depth of the deepest directory - d4).
A quick query posted on the local linux user group got me the solution


 find . -printf '%d\n' | sort -n  | tail -1


Works like a charm!

For those times when the -printf switch is not available for the find command (for example if you are running busybox instead of bash), here is a handy alternative -

find -type d | awk -F'/' '{print NF-1 "\n"}' | sort -n | tail -1


But this will not work for absolute directory names as then the depth of the base path will also be considered.

So here is the working solution for busybox users -

basedir="/home/something/"
find "$basedir" | sed "s#$basedir##g" | awk -F'/' '{print NF}' | sort -n | tail -1


However, if one is using gawk, or a modern awk, one can get it to replace sed (and, with some more work, probably sort, and tail also), viz.

basedir="/home/something/"
find "$1" | gawk -vbasedir="$1" -F/ '{gsub(basedir, "", $0); print NF-1}' | sort -n | tail -1


Great!