Can this recursive haskell function be included in a card call?

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 am looking for a way to use a map function and get rid of this recursive function. Is this possible, and if so, how to do it?

+7
recursion haskell
source share
2 answers

Look at the map type. This is (a -> b) -> [a] -> [b] . This is not like your type, which is [a] β†’ b. This is not a map, this is a fold.

The higher order function you want to see is foldr . See Hoogle .

Something like...

 htmlHAtendimento :: [HoraAtendimento] -> Html htmlHAtendimento [] = toHtml "" htmlHAtendimento l = foldr1 (+++) $ intersperse ", " $ map fl where f (da, hia, hfa) = toHtml da +++ "feira " +++ show hia +++ "h - " +++ show hfa +++ "h" 

I do not know if this is right, but it is in the right direction.

+12
source share

You want to drop a non-empty list. This code can 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" 
+2
source share

All Articles