ワンライナーっぽく出来た。
Prelude> let addComma numStr = concat $ snd $ foldr (\x t ->(fst t + 1,(if fst t `mod` 3 == 0 && fst t /= 0 then (x++",") else x):snd t)) (0,[]) [[x]|x <-numStr] Prelude> addComma "1234" "1,234" Prelude> addComma "123" "123" Prelude> addComma "1234567890123" "1,234,567,890,123"
こっちの方が短い。
addComma numStr = concat $ snd $ foldr (\x t ->(fst t + 1,(if fst t `mod` 3 == 0 && fst t /= 0 then ([x]++",") else [x]):snd t)) (0,[]) numStr
> concat $ reverse$zipWith (\x y ->if x=="" then y else x++y) (take (length num)(cycle ["","","",","])) (reverse $map (\x->[x]) num) -- > "12,3456,7890,1234,5678,9012,3456,7890"
> let f = snd.foldr (\x (n,xs)->if n>0 && n `mod`3==0 then (n+1,x:',':xs) else (n+1,x:xs)) (0,"") > f "1234567" -- > "1,234,567"
- 2011/11/07 追記
let comma s = concat $ reverse [[n]++c|(c,n)<- zip ("":(cycle ["","",","])) (reverse s)] > comma "123456789" -- > "123,456,789"