如何在Haskell中获取在线文档?
有没有像Python下面那样优雅/方便的东西?
>>> help([].count)
Help on built-in function count:
count(...)
L.count(value) -> integer -- return number of occurrences of value
Run Code Online (Sandbox Code Playgroud) 我真的不明白.为什么我们需要呢?我的意思是如果我使用相同的类型参数,我认为这意味着它们应该是相同的类型.
我听说它可以帮助编译器避免无限循环.有人可以告诉我一些更多细节吗?
最后,在Real World Haskell中我们应该遵循功能依赖的使用吗?
[后续问题]
class Extract container element where
extract :: container -> element
instance Extract (a,b) a where
extract (x,_) = x
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我对容器和元素都使用了相同的类型变量'a',我认为编译器因此可以推断这两种类型是相同的类型.
但是当我在GHCi中尝试这个代码时,我收到了以下反馈:
*Main> extract('x',3)
<interactive>:1:0:
No instance for (Extract (Char, t) element)
arising from a use of `extract' at <interactive>:1:0-13
Possible fix:
add an instance declaration for (Extract (Char, t) element)
In the expression: extract ('x', 3)
In the definition of `it': it = extract ('x', 3)
Run Code Online (Sandbox Code Playgroud)
当其中一个被指定为类型'Char'时,为什么另一个仍未解析类型'element'?
haskell types type-systems typeclass functional-dependencies
我有两个功能:
emptyDirectory, copyStubFileTo :: FilePath -> IO ()
Run Code Online (Sandbox Code Playgroud)
我想按照以下方式组合它们:
forM_ ["1", "2"] $\n -> do
emptyDirectory n
copyStubFileTo n
Run Code Online (Sandbox Code Playgroud)
Haskell中是否还有其他标准方法来简化这种组合?我的意思是加入两个IO动作并给它们相同的输入.
根据CAP理论,Cassandra最终只能具有一致性.更糟糕的是,如果我们在一次请求中有多次读写而没有正确处理,我们甚至可能失去逻辑一致性.换句话说,如果我们快速做事,我们可能做错了.
同时,为Cassandra设计数据模型的最佳实践是考虑我们将要拥有的查询,然后添加一个CF. 通过这种方式,添加/更新一个实体意味着在许多情况下更新许多视图/ CF. 没有原子事务功能,很难做到正确.但有了它,我们又失去了A和P部分.
我不认为这涉及很多人,因此我想知道为什么.
在Perl中,我可以这样做:
push(@{$h->[x]}, y);
Run Code Online (Sandbox Code Playgroud)
我可以根据上面的Perl示例简化以下python代码吗?
if x not in h:
h[x] = []
h[x].append(y)
Run Code Online (Sandbox Code Playgroud)
我想简化这个,因为它在我的代码中有很多地方,(我不能用[]初始化所有可能的x).我不想让它成为一个函数,因为没有'inline'关键字.
有任何想法吗?
在GHCi中:
- 序曲>(+3)2
5- 序曲>(*3)2
6- 前奏>(/ 3)2
0.6666666666666666- 前奏>( - 3)2
没有实例(Num(t - > t1))
来自文字':it =( - 3)23' at <interactive>:1:2
Possible fix: add an instance declaration for (Num (t -> t1))
In the expression: 3
In the expression: (- 3) 2
In the definition of
如何更正最后一个使其返回-1?
我正在尝试<Real World Haskell>的代码.
在GHC版本6.10.4上:
data ParseState = ParseState {
string :: String
} deriving (Show)
newtype Parse a = Parse {
runParse :: ParseState -> Either String (a, ParseState)
}
parse :: Parse a -> String -> Either String a
parse parser initState =
case runParse parser (ParseState initState) of
Left err -> Left err
Right (result, _) -> Right result
Run Code Online (Sandbox Code Playgroud)
一切顺利,直到我将'Left err'改为'err @(Left _)':
-- err@(Left _) -> err
{-
- Occurs check: cannot construct the infinite type:
- …Run Code Online (Sandbox Code Playgroud) guard :: (MonadPlus m) => Bool -> m ()
guard True = return ()
guard False = mzero
Prelude Control.Monad> :t mzero
mzero :: (MonadPlus m) => m a
Run Code Online (Sandbox Code Playgroud)
在False分支中guard,类型为mzerois m a,但返回类型guard已被指定为m ().因此我不太明白为什么编译器不会抱怨这个.
我的意思是如果mzero返回一个键入的值Maybe Int,当然,这是不同的Maybe (),对吗?
请参阅以下代码:
def good():
foo[0] = 9 # why this foo isn't local variable who hides the global one
def bad():
foo = [9, 2, 3] # foo is local, who hides the global one
for func in [good, bad]:
foo = [1,2,3]
print('Before "{}": {}'.format(func.__name__, foo))
func()
print('After "{}": {}'.format(func.__name__, foo))
Run Code Online (Sandbox Code Playgroud)
结果如下:
# python3 foo.py
Before "good": [1, 2, 3]
After "good": [9, 2, 3]
Before "bad" : [1, 2, 3]
After "bad" : [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)