是否可以将此递归haskell函数转换为地图调用?

Ric*_*ral 7 recursion haskell

这是我的代码:

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
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法来使用map函数并摆脱这种递归函数.这是可能的,如果是的话,我该怎么做?

Apo*_*isp 12

看看的类型map.是的(a -> b) -> [a] -> [b].这看起来不像你的类型,这是[a] - > b.那不是地图,这是一个折叠.

您要查看的高阶函数是foldr.见Hoogle.

就像是...

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"
Run Code Online (Sandbox Code Playgroud)

我不知道这是否正确,但这是正确的方向.