Sunday, August 2, 2009

Obnoxious Schemeing :P(Brute Forcer in Scheme)

Aah the woes of BruteForcing!!! I was racking my brain for some time to code a very nice bruteforcing algo. I had some ideas but they were all messy so had to stop. Finally i found one googling. It was awesome simply awesome. It was written in c# and i thought I too should get an implementation of the same in some other language that has not yet been implemented. So here's my shot at Fame :P.

I am in love with scheme. To me its the best language where you can understand looping(The thing we take for granted thanks to the For Loop). Its also the best language for understanding recursion. Recursion in scheme is simply superb!!!! So here's a little implementation of that awesome bruteforcing algo in scheme translated by me.

(define sb "")
(define charlst '(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m #\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z))
(define (Start length)
(let loop([i 0] [max length])
[(= i max) (IterateChars 0 sb length)]
[else (set! sb (string-append sb "a"))
(loop (+ i 1) max)])))
(define (IterateChars pos sb length)
(let loop([i 0] [max 26])
[(= i max) (display "")]
[else (string-set! sb pos (list-ref charlst i))
[(= pos (- length 1)) (display "\n") (display sb)]
[else (IterateChars (+ pos 1) sb length)])
(loop (+ i 1) max)])))
(Start 4)

In order to change the the length of the string just change (Start length) => length to desired choice. I must simply say that anyone who wants to understand Looping and Recursion better should give scheme a shot.

[More Details On Scheme]

[Get Compiler Here]

1 comment:

andrewl said...

WTF is that!! I happen to like my for loops :)

You should take some very basic brutable algorithm (like maybe WinFan's recent submission) and implement the bruter in both scheme and C.

If you run each one several times to get an accurate measure of typically how they take to finish, I wonder which proves faster.

Good job going and learning new things (like Java in recent post). It is so easy to just sit idle and comfortable with what we already know.