So I'm trying to write a function which performs a input function f on input list ls [l1, l2, ..., ln] and output a string "[" ++ (f l1) ++ "," ++ (f l2) ++ "," ++ ... ++ (f ln) ++ "]"
flist :: (a -> String) -> [a] -> String
flist f ls =
for example:
>flist show [1, 2, 3]
would output "[1, 2, 3]"
>flist (fun x -> x) ["dog"]
would output "[dog]"
I tried to use foldl'
flist f ls = "[" ++ (foldl' (++) f "," ls) ++ "]"
which doesn't seems to be working
Hint:
[f x1,...,f xn] first, applying f to every member.[y1,...,yn] and w and produces an interleaving [y1,w,y2,w,...,yn]. This can be done by recursion. (There's also a library function for that, but it's not important.)[f x1, ",", ...] and then concatenate the result.How about this:
import Data.List
flist f list = "[" ++ (intercalate "," $ map f list) ++ "]"
intercalate puts Strings in between of other Strings, it is (in this case) a slightly configurable version of unwords.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With