要从GHCi中的列表中删除最后一项,我可以反转列表,取尾,然后再将其反转.例如,
reverse(tail(reverse([1,2,3,4])))
Run Code Online (Sandbox Code Playgroud)
因为有很多括号,我想我会改变它来使用功能组合.但是,当我尝试这个时,我得到以下错误.
Prelude> reverse . tail. reverse [1,2,3,4]
<interactive>:2:17:
Couldn't match expected type `a0 -> [a1]' with actual type `[a2]'
In the return type of a call of `reverse'
Probable cause: `reverse' is applied to too many arguments
In the second argument of `(.)', namely `reverse [1, 2, 3, 4]'
In the second argument of `(.)', namely
`tail . reverse [1, 2, 3, 4]'
Run Code Online (Sandbox Code Playgroud)
我认为这意味着它不喜欢作曲reverse [1,2,3,4],所以我尝试在它周围放置括号,但它给了我同样的错误.
Prelude> reverse . tail. (reverse [1,2,3,4])
<interactive>:3:18:
Couldn't match expected type …Run Code Online (Sandbox Code Playgroud) 它假设是功能组合.我认为问题是当funcs中只剩下一个函数时.我希望它是一个空元组,但它没有像那样识别并进入无限循环
谢谢!:)
def compose(*funcs):
if len(funcs)==0:
return lambda x:x
f=funcs[0]
return lambda x: f(compose(funcs[1:])(x))
Run Code Online (Sandbox Code Playgroud) 我目前正在处理99个haskell问题
我不明白为什么我在这个函数中出错了: -
repli :: [a] -> Int -> [a]
repli xs n = concatMap (take n . repeat) xs
Run Code Online (Sandbox Code Playgroud) 例如,F#是否有可能在Operators.Not某些标准.NET函数之间具有函数组合String.IsNullOrEmpty?
换句话说,为什么以下lambda表达式是不可接受的:
(fun x -> not >> String.IsNullOrEmpty)
Run Code Online (Sandbox Code Playgroud) lambda f# functional-programming function function-composition
(deftask test1 "first test task" [] (print "1") identity)
(deftask test2 "second test task" [] (print "2") identity)
(boot (comp (test1) (test2)))
=> 12nil
(boot (comp (fn [x] (print "1") identity) (fn [x] (print "2") identity)))
=> 21nil
Run Code Online (Sandbox Code Playgroud)
如果我使用comp任务,执行顺序是从左到右.如果我使用comp匿名函数,则执行顺序是从右到左.这种不一致性如何合理?
在Functor类定义中,我们将<$函数定义为:
class Functor f where
fmap :: (a -> b) -> f a -> f b
(<$) :: a -> f b -> f a
(<$) = fmap . const
Run Code Online (Sandbox Code Playgroud)
该const函数具有以下定义:
const :: a -> b -> a
const x _ = x
Run Code Online (Sandbox Code Playgroud)
我知道这个<$功能相当于:
\x -> fmap (const x)
Run Code Online (Sandbox Code Playgroud)
如何fmap . const等同于上面的lambda表达式?我的函数组合的理解是该输出类型const应该匹配的输入类型fmap,但输入型fmap是功能(a -> b)不a这是什么const功能输出.
我已经读过g :: A - > B和f :: B - > C的组成,发音("f由g组成"),导致另一个函数(箭头)来自A - > C.这可以是更正式地表达为
f•g = f(g)= compose ::(B - > C) - >(A - > B) - >(A - > C)
上述组成也可以定义如下吗?请澄清.在这种情况下,compose函数采用相同的两个函数f和g并从A - > C返回一个新函数.
f•g = f(g)= compose ::((B - > C),(A - > B)) - >(A - > C)
javascript haskell functional-programming composition function-composition
您好,有人可以从功能组合的Real World Haskell向我解释这个例子:
data Doc = ToBeDefined deriving (Show)
(<>) :: Doc -> Doc -> Doc
a <> b = undefined
series :: Char -> Char -> (a -> Doc) -> [a] -> Doc
series open close item = enclose open close
. fsep . punctuate (char ',') . map item
-- Who does fsep compose with?
enclose :: Char -> Char -> Doc -> Doc
enclose begin end input = char begin <> input <> char <> end
Run Code Online (Sandbox Code Playgroud)
我不明白谁是 …
我有我的代码的简化版本。显而易见,我在概念上想要什么:
def heavyCalcMul: Int => Int = i => i * 2
def heavyCalcDiv: Int => Int = i => i / 2
def heavyCalcPls: Int => Int = i => i + 2
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
val x = 2
val midResult = heavyCalcMul(x)
val result = heavyCalcDiv(midResult) + heavyCalcPls(midResult)
Run Code Online (Sandbox Code Playgroud)
但是我想用这种风格重写这段代码:
val x = 2
val result = heavyCalcMul(x) { midResult: Int =>
heavyCalcDiv(midResult) + heavyCalcPls(midResult)
}
Run Code Online (Sandbox Code Playgroud)
可能吗?
我正在从“学习Haskell为伟大!”一书中学习单子。由Miran Lipovaca撰写。我试图理解单子的结合律。本质上,法律规定,当您使用拥有一元函数应用程序链时>>=,如何嵌套它们无关紧要。
以下代码使人们能够将type函数的结果传递给type a -> m b函数b -> m c:
(<=<) :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c)
f <=< g = (\x -> g x >>= f)
Run Code Online (Sandbox Code Playgroud)
但是,对于以下示例:
ghci> let f x = [x, -x]
ghci> let g x = [x*3, x*2]
ghci> let h = f <=< g
ghci> h 3
[9, -9, 6, -6]
Run Code Online (Sandbox Code Playgroud)
是f x和g x两个功能?看来它们是具有不同x值而不是函数的列表。该行在 …