[Haskell-beginners] Shorten this code

Henk-Jan van Tuyl hjgtuyl at chello.nl
Wed Jul 24 15:18:34 CEST 2013

On Tue, 23 Jul 2013 12:41:32 +0200, Nadav Chernin <nadavchernin at gmail.com>  

> Hi, all
> Below is my solution to SPOJ->Polybius
> square<http://www.spoj.com/problems/POLYBIUS>
> Please try to shorten it:
> *import Data.Maybe*
> *d=[1..5]*
> *f s=unwords$map(\c->fromJust$lookup c(('
> ',""):('J',"24"):zip(['A'..'I']++['K'..'Z'])[show(x+10*y)|y<-d,x<-d]))s*
> *main=getLine>>(interact$unlines.map f.lines)*

If you remove the unnecessary spaces from the following code, it is  
shorter than yours:

f s = unwords $ map (\c -> let (Just x) = lookup c ((' ',"") : ('J',"24")  
: zip (['A'..'I'] ++ ['K'..'Z']) [y : [x] | y <- d, x <- d]) in x) s
main = getLine >> (interact $ unlines . map f . lines)

Note, that this is not a real solution to the problem, as you don't do  
anything with the first input.

Henk-Jan van Tuyl

Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.

Haskell programming

More information about the Beginners mailing list