[Haskell-beginners] shortening code

Michael Mossey mpm at alumni.caltech.edu
Tue Jun 29 23:25:18 EDT 2010



rizwan hudda wrote:
> I have recently started learning haskell, and was trying to write a code 
> for this problem http://www.spoj.pl/problems/KAMIL in haskell. The aim 
> is to write a shortest possible code for the given task. I have 
> previously solved this in c,c++,perl,python.My best was 57 characters 
> using perl. So here's my code in HASKELL:
> 
> main = interact $ unlines. map (show.foldl (\a x-> if null $ filter 
> (==x) "TDLF" then a else a+a) 1) .lines
> 
> It is 107 characters [ non white space and newline ]. I was interested 
> in knowing how i could further optimize the size of this code.

This kind of contest is fun, but I can't help thinking I hope you are 
learning Haskell to appreciate the things that functional programming is 
good at. Real-world Haskell is often more concise than other languages 
because Haskell has mechanisms for eliminating unnecessary and redundant 
code, but Haskell's strength is not winning "shortest program" contests.

Mike


More information about the Beginners mailing list