作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个功能:
fun1 :: Int -> [Int]
fun2 :: [Int] -> [Int]
fun2
接受 Int list
并使用 map
帮助将 fun1
应用于此列表的每个元素。但是fun1
返回[Int]
。所以,我有类型冲突。如何解决我的问题?
最佳答案
您可能需要结合使用 map
和 concat
来实现它。假设 fun1
和 fun2
是这样的:
fun1 :: Int -> [Int]
fun1 x = [x,x]
fun2 :: [Int] -> [Int]
fun2 = map (+ 1)
solution :: [Int] -> [Int]
solution xs = concat $ map fun1 (fun2 xs)
或者按照@CarstenKonig的建议,您可以使用concatMap
solution2 :: [Int] -> [Int]
solution2 xs = concatMap fun1 $ fun2 xs
可以进一步简化为:
solution2 :: [Int] -> [Int]
solution2 = concatMap fun1 . fun2
关于function - 如何在map函数中连接列表-Haskell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29965417/
我是一名优秀的程序员,十分优秀!