This is my code:
type HoraAtendimento = (String, Int, Int)
htmlHAtendimento :: [HoraAtendimento] -> Html
htmlHAtendimento [] = toHtml ""
htmlHAtendimento ((da,hia,hfa):[]) = toHtml da +++ "feira "
+++
show hia +++ "h - " +++ show hfa +++ "h"
htmlHAtendimento ((da,hia,hfa):r) = toHtml da +++ "feira "
+++
show hia +++ "h - " +++ show hfa +++ "h, "
+++
htmlHAtendimento r
I'm looking for a way to use the map function and get rid of this recursive function. Is that possible and if so, how do I do it?
Look at the type of map
. It is (a -> b) -> [a] -> [b]
. That doesn't look like your type, which is [a] -> b. That's not a map, that's a fold.
The higher-order function you want to look at is foldr
. See Hoogle.
Something like...
htmlHAtendimento :: [HoraAtendimento] -> Html
htmlHAtendimento [] = toHtml ""
htmlHAtendimento l = foldr1 (+++) $ intersperse ", " $ map f l
where f (da, hia, hfa) = toHtml da
+++ "feira "
+++ show hia
+++ "h - "
+++ show hfa
+++ "h"
I don't know if that's correct, but that's in the right direction.
You want to fold over a nonempty list. This code might do the trick:
type HoraAtendimento = (String, Int, Int)
htmlHAtendimento :: [HoraAtendimento] -> Html
htmlHAtendimento [] = toHtml ""
htmlHAtendimento l = foldl1 (+++) $ map convert l
where convert (da,hia,hfa) = toHtml da +++ "feira " +++
show hia +++ "h - " +++ show hfa +++ "h"
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