我还在学习Haskell,我正在做一些练习,但我一直在困惑。所以我有一个叫做“novel”的函数,它的参数需要2个字符串和一个Int (novel :: (String, String, Int) -> String
)。新的输入/输出必须如下所示:Haskell中的递归
> novel ("Rowling", "Harry Potter", 1998)
"Harry Potter (Rowling, 1998)"
这是我对我的小说的功能代码作品如上解释说:
novel :: (String, String, Int) -> String
novel (author, book, year) = book ++ " (" ++ author ++ ", " ++ (show year) ++ ")"
我试图写一个所谓的新功能,“举“(cite :: [(String, String, Int)] -> String
)。引用的输入/输出应如下所示:
> cite [("author1", "book1", year1), ("author2", "book2", year2), ("author3", "book3", year3)]
"book1 (author1, year1)
book2 (author2, year2)
book3 (author3, year3)"
我试图用“小说,”递归,为了得到所需的输出,但我不知道如何去这件事。
我已经试过:
cite :: [(String, String, Int)] -> String -- | Listed arguments
cite [] = "" -- | Base Case
cite x:xs = [(novel (author, book, year)), (novel (author, book, year)), (novel (author, book, year))]
这是诚实的,据我得到了。显然,它不起作用,但我不确定在这里做什么。
提示:查看['map'](http://hackage.haskell.org/package/base-4.7.0.2/docs/src/GHC-Base.html#map)获取想法。 – bheklilr
是否希望它返回在每个引用之间使用“\ n”的'String'(即'[Char]'),还是要返回'[String]'? – TheCriticalImperitive
我不明白地图如何帮助我。我对map的理解是,它需要一个函数和一个列表,并且可以用算术运算来处理该列表中的项目。 –