haskell - Using intersperse and show -


i'm trying code:

intersperse ',' $ show 123 

integrated function:

printlisttuples listtuple = unlines [ ys ++ " " ++ unwords (map show x) | (x, ys) <- listtuple ] 

where x can equal 123 example.

therefore code should output string numbers this: "1,2,3"

rather "1 2 3" code does.

i keep getting errors trying "map" "intersperse" , "show" x in function. advice?

edit:

for example, tried using

printlisttuples listtuple = unlines [ ys ++ " " ++  unwords (map intersperse ',' $ show x) | (x, ys) <- listtuple ] 

but returns error:

    couldn't match expected type ‘string -> [string]’                 actual type ‘[[a0] -> [a0]]’     first argument of ($) takes 1 argument,     type ‘[[a0] -> [a0]]’ has none     in first argument of ‘unwords’, namely       ‘(map intersperse ',' $ show x)’     in second argument of ‘(++)’, namely       ‘unwords (map intersperse ',' $ show x)’      couldn't match expected type ‘[a0]’ actual type ‘char’     in second argument of ‘map’, namely ‘','’     in expression: map intersperse ','     in first argument of ‘unwords’, namely       ‘(map intersperse ',' $ show x)’ 

which not know enough fix.

let's have @ first error:

couldn't match expected type ‘string -> [string]’             actual type ‘[[a0] -> [a0]]’ first argument of ($) takes 1 argument, type ‘[[a0] -> [a0]]’ has none in first argument of ‘unwords’, namely   ‘(map intersperse ',' $ show x)’ 

"the first argument of ($) takes 1 argument" -- means should function, type shows:

ghci> :t ($) ($) :: (a -> b) -> -> b 

map intersperse ',', however, not function. passing 2 arguments map, list...

ghci> :t map map :: (a -> b) -> [a] -> [b] 

... however, map intersperse ',' ill-typed: ',' not list, cannot use map map function on (that other error says). root of issue don't need map apply intersperse ',' string:

ghci> :t intersperse ',' intersperse ',' :: [char] -> [char] 

that fix 2 errors... , introduce another:

ghci> let printlisttuples listtuple = unlines [ ys ++ " " ++  unwords (intersperse ',' $ show x) | (x, ys) <- listtuple ]  <interactive>:33:66:     couldn't match type ‘char’ ‘[char]’     expected type: [string]       actual type: [char]     in first argument of ‘unwords’, namely       ‘(intersperse ',' $ show x)’     in second argument of ‘(++)’, namely       ‘unwords (intersperse ',' $ show x)’     in second argument of ‘(++)’, namely       ‘" " ++ unwords (intersperse ',' $ show x)’ 

now unwords wants list of strings, giving intersperse ',' $ show x, list of chars (that is, string). directs our attention fact not need unwords anymore: used put space between numbers, want put comma instead of space, , doing intersperse. have removing it:

ghci> let printlisttuples listtuple = unlines [ ys ++ " " ++  (intersperse ',' $ show x) | (x, ys) <- listtuple ] 

et voilĂ :

ghci> printlisttuples [(123, "foo"), (456, "bar")] "foo 1,2,3\nbar 4,5,6\n" 

from here, should decompose function smaller parts, user5402 suggests in answer.


Comments