
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. Thanks in advance. -- Thanks and regards Rizwan A Hudda http://sites.google.com/site/rizwanhudda

(Sorry, rizwan, first e-mail wasn't sent to the list)
On Tue, Jun 29, 2010 at 8:35 PM, rizwan hudda
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.
Well, we have: null (filter (== x) cs) = x `notElem` cs = not (x `elem` cs) So we get: main = interact $ unlines . map (show . foldl (\a x -> if elem x "TDLF" then a+a else a) 1) . lines I'm not looking at the problem, but if I understood the line above correctly, each line should print 2^n where n is the number of elements TDLF in that line: main = interact $ unlines . map (show . (2^) . length . filter (`elem` "TDLF")) . lines That's 70 non-whitespace characters. This one-liner is pretty readable, probably more than the Perl one ;D. Cheers, -- Felipe.

Felipe Lessa schrieb:
main = interact $ unlines . map (show . (2^) . length . filter (`elem` "TDLF")) . lines
That's 70 non-whitespace characters. This one-liner is pretty readable, probably more than the Perl one ;D.
May I toss in: main = getLine >>= print . (2 ^) . length . filter (`elem` "TDLF")
main
(60 non-whitespace characters) C.

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

I agree. If you're aiming for really terse code, have a look at the
J/K languages. These seem to win most golf-shootout type competitions.
Haskell is more focused on expressiveness and abstraction.
On Wed, Jun 30, 2010 at 11:25 AM, Michael Mossey
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 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thanks Felipe Lessa for shortening the code :) and everyone for advice.
I have recently started learning haskell from the book
Learnyouahaskell - www.learnyouahaskell.com - I am done with the book except
for Bytestreams and randomness. Was trying to put my haskell skills to use
on the problems of this kind. My aim is to understand and practice haskell
and not WIN these code golf competitions! . So, please suggest me what are
the other books i can read ( and work through :) ), in general any good open
source projects to involve in, etc.. basically any advices to improve as a
haskell programmer.
Thanks in advance.
On Wed, Jun 30, 2010 at 9:50 AM, Lyndon Maydwell
I agree. If you're aiming for really terse code, have a look at the J/K languages. These seem to win most golf-shootout type competitions. Haskell is more focused on expressiveness and abstraction.
On Wed, Jun 30, 2010 at 11:25 AM, Michael Mossey
wrote: 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 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Thanks and regards Rizwan A Hudda http://sites.google.com/site/rizwanhudda
participants (5)
-
Christian Maeder
-
Felipe Lessa
-
Lyndon Maydwell
-
Michael Mossey
-
rizwan hudda