以下代码提出了UnboundLocalError:
def foo():
i = 0
def incr():
i += 1
incr()
print(i)
foo()
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
一个简单的递归因子方法非常有效:
def fact(n):
if n == 0:
return 1
return n * fact(n-1)
Run Code Online (Sandbox Code Playgroud)
但是我想尝试一下并使用一个dict代替.从逻辑上讲,这应该可行,但是一堆打印语句告诉我n,不是停止,而是0向下滑过负数,直到达到最大递归深度:
def recursive_fact(n):
lookup = {0: 1}
return lookup.get(n, n*recursive_fact(n-1))
Run Code Online (Sandbox Code Playgroud)
这是为什么?
在Java或C中,我们将<condition> ? X : Y其转换为Python X if <condition> else Y.
但也有这个小技巧:<condition> and X or Y.
虽然我明白,这是相当于前述三元运营商,我觉得很难把握如何and和or运营商能够产生正确的结果.这背后的逻辑是什么?
遵循 Church 编码的自然数和表示为右折叠的列表的标准定义,我想编写一个函数,它将数字列表作为其参数并返回其元素的总和:
type Number = forall a. (a -> a) -> a -> a
type List a = forall b. (a -> b -> b) -> b -> b
zero :: Number
zero _ x = x
plus :: Number -> Number -> Number
plus a b f x = a f (b f x)
sum :: List Number -> Number
sum xs = xs plus zero
Run Code Online (Sandbox Code Playgroud)
这个定义sum不进行类型检查 - 我认为这是因为它的类型扩展到
(forall a. (a -> a) -> a …Run Code Online (Sandbox Code Playgroud) 简而言之,我希望在 Haskell 中进行以下类型检查:
\nboth f (x, y) = (f x, f y)\n\nfoo :: ([Int], [Char])\nfoo = ([1], "a")\n\nbar :: ([Int], [Char])\nbar = both (concat . replicate 3) foo -- intended: ([1, 1, 1], "aaa")\nRun Code Online (Sandbox Code Playgroud)\n我收到的错误是:
\n\xe2\x80\xa2 Couldn\'t match type \xe2\x80\x98Char\xe2\x80\x99 with \xe2\x80\x98Int\xe2\x80\x99\n Expected: ([Int], [Int])\n Actual: ([Int], [Char])\n\xe2\x80\xa2 In the second argument of \xe2\x80\x98both\xe2\x80\x99, namely \xe2\x80\x98foo\xe2\x80\x99\n In the expression: both (concat . replicate 3) foo\n In an equation for \xe2\x80\x98bar\xe2\x80\x99: bar = both (concat . replicate 3) …Run Code Online (Sandbox Code Playgroud) 所以我几天前就开始学习Java了,除了这个令我难以置信的练习外,我的表现非常好.因此,练习是"编写一个程序,显示从1到30不可分割的所有数字3".所以这很容易:
class numbers {
public static void main (String args[]) {
for (int i = 0; i <=30; i++){
switch(i % 3){
case 0 :
break;
default :
System.out.println(i);
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
除了其中一个变体之外," break检测到3之后的可用性.现在我不确定break上面的代码中使用的是否是正确的,因为它是其中的一部分switch.我想知道是否有其他方法可以做到这一点.
我正在尝试使用正则表达式删除文本中的所有标点符号.问题是,标点符号正则表达式似乎没有任何影响(既不是\p{P}也没有\p{Punct}).
import re
hello_world = 'Hello, world!'
hello_world = re.sub('\p{Punct}', '', hello_world)
print(hello_world)
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?以下产生了预期的效果,但我仍然不明白为什么上面的代码不起作用.
# import string
# ...
hello_world = re.sub('[{}]'.format(string.punctuation), '', hello_world)
Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的单子堆栈:
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Except
import Control.Monad.Trans
type MyMonad = ReaderT Env (ExceptT String IO) -- Env is irrelevant
Run Code Online (Sandbox Code Playgroud)
和一个函数(简化,但想法成立):
f :: Integer -> MyMonad Integer
f 42 = lift $ throwE "42 is an ILLEGAL number"
f n = return n
Run Code Online (Sandbox Code Playgroud)
我现在想做的是f从另一个函数调用,但如果发生则捕获抛出的异常并以某种方式处理它(例如,抛出另一个异常但消息已更改)。我很难弄清楚应该在这里进行什么样的电梯操作才能正确完成。我尝试过这样的事情:
g n = do
x <- (f n) `catchE'` (\_ -> lift $ throwE "nope, still illegal")
return x
where catchE' - lift . catchE
Run Code Online (Sandbox Code Playgroud)
但它显然行不通,因为catchE'在 monad 中需要一些东西ExceptT,而不是MyMonad. 可以轻松完成吗?也许改变 …
我使用 4 个常见的 Python 类型检查器测试了以下代码片段,令我惊讶的是,它们都没有抱怨:
\nfrom typing import Any\n\ndef length(s: str) -> int:\n return len(s)\n\ndef any_length(o: Any) -> int:\n return length(o)\n\nif __name__ == "__main__":\n print(any_length(1234))\nRun Code Online (Sandbox Code Playgroud)\n很容易预测运行此代码将导致异常:
\nTypeError: object of type \'int\' has no len()\nRun Code Online (Sandbox Code Playgroud)\n我的:
\nSuccess: no issues found in 1 source file\nRun Code Online (Sandbox Code Playgroud)\npy类型:
\nSuccess: no errors found\nRun Code Online (Sandbox Code Playgroud)\n皮赖特:
\n0 errors, 0 warnings, 0 informations\nCompleted in 0.435sec\nRun Code Online (Sandbox Code Playgroud)\n柴堆:
\n\xc6\x9b No type errors found\nRun Code Online (Sandbox Code Playgroud)\n我期望至少有一个警告说它 …
使用时写入txt文件时如何插入新行java.nio.file?下面的代码产生具有一个线txt文件ABCDEF,而不是两个单独的行ABC和DEF:
public static void main(String args[]) throws IOException {
final Path PATH = Paths.get("test.txt");
String test = "ABC\nDEF";
Files.write(PATH, test.getBytes());
}
Run Code Online (Sandbox Code Playgroud)