compress xs@(_:_:_) = (ifte <$> ((==) <$> head <*> head.tail) <$> ((compress.).(:) <$> head <*> tail.tail) <*> ((:) <$> head <*> compress.tail) ) xs
导致类型错误,但我不明白为什么.它应该相当于
compress xs@(_:_:_) = (ifte (((==) <$> head <*> head.tail) xs) (((compress.).(:) <$> head <*> tail.tail) xs) (((:) <$> head <*> compress.tail) xs))
,没有.
注意:ifte = (\ x y z -> if x then y else z),<$>并<*>从Control.Applicative.
编辑:错误是:
Couldn't match expected type `[a]' with actual type `[a] …Run Code Online (Sandbox Code Playgroud) 我想快速正确地减少函数以在Haskell中指向自由格式.我更愿意产生相当可读的结果.我该怎么办呢?
为什么第一个失败而后者成功编译?
我期望foo并且foo'是等价的,也就是说,foo'它只是一个无点函数foo:
foo :: [a] -> [a] -> [(a,a)]
foo = map id . zip
foo' :: [a] -> [a] -> [(a,a)]
foo' a b = map id $ zip a b
Run Code Online (Sandbox Code Playgroud)
但foo失败并出现以下错误:
Couldn't match type ‘[b0] -> [(a, b0)]’ with ‘[b]’
Expected type: [a] -> [b]
Actual type: [a] -> [b0] -> [(a, b0)]
Relevant bindings include
foo :: [a] -> [b] (bound at <interactive>:26:5)
Probable cause: ‘zip’ is …Run Code Online (Sandbox Code Playgroud) 我刚学会了Haskell中的无点样式,以及它如何帮助整理代码并使其更易于阅读.但有时他们可以使代码有点过于简洁.
所以,当我应该总是使用无点样式时,在什么情况下我应该绝对避免在Haskell中使用无点样式?
我已经编写了这个函数map,但是我需要使用list comprehension来编写它:
alter = map (\x -> if x == 0 then 1 else 0)
Run Code Online (Sandbox Code Playgroud)
它给出了例如
alter [1,1,0]
> [0,0,1]
Run Code Online (Sandbox Code Playgroud) 假设我要计算平均费用:
const products = [
{
cost: 300
},
{
cost: 700
}
];
Run Code Online (Sandbox Code Playgroud)
因此,首先选择成本属性,对其进行汇总,然后除以项目的nr个。
const calcualteAveragePrice = R.pipe(
R.map(R.prop('cost') // [300, 700]
R.sum, // 1000
R.divide(??) // How do I divide with the number of items here??
)
Run Code Online (Sandbox Code Playgroud)
在最后一步中,我需要除以项目数。由于它是免费的,所以我不能arr.length。
我正在努力正确定义该函数的无点版本,它将向列表添加 2 个元素。
\n很容易想出一些简单的简单实现:
\naddTwoElems :: a -> a -> [a] -> [a]\n\naddTwoElems x y xs = x : y : xs\naddTwoElems x y = (++) [x, y]\naddTwoElems = (.) `on` (:) \xe2\x80\x9c point free but with additional function\nRun Code Online (Sandbox Code Playgroud)\n但是两个列表数据构造函数的无点组合(:)(.) 会是什么样子呢?
请不仅展示正确的功能实现,还请解释如何获得正确版本的步骤和逻辑。
\nsyntax haskell functional-programming pointfree type-constructor
我有一个像这样的 Haskell 函数:
in2out :: String -> String
in2out s =
show (sumAllLineValues $ lines s)
Run Code Online (Sandbox Code Playgroud)
(其中sumAllLineValues在我的代码中定义。)
我如何定义in2out无点,所以没有参数s?
如何以无点样式重写以下函数,x完全从定义中删除参数(另外两个可能保留):
between min max x = (min < x) && (x < max)
Run Code Online (Sandbox Code Playgroud)
这不是一项任务,只是一个问题.我不知道该怎么办.我可以把它变成一个lambda函数
between min max = \x -> (min < x) && (x < max)
Run Code Online (Sandbox Code Playgroud)
但这不是没有点的,因为x它仍然存在.请帮忙.