我遇到了这个Python程序的问题我正在创建数学,工作和解决方案,但我得到语法错误:"python中的行后续字符意外的字符"
这是我的代码
print("Length between sides: "+str((length*length)*2.6)+" \ 1.5 = "+str(((length*length)*2.6)\1.5)+" Units")
Run Code Online (Sandbox Code Playgroud)
我的问题是\ 1.5我试过\ 1.5但它不起作用
使用python 2.7.2
我在Ruby世界中相对较新.我不知道,该怎么想.在"Ruby编程语言"中,我读过我不应该在新代码中使用Continuations而是使用Fibers.我发现这个演示文稿(来自2008年)http://www.atdot.net/~ko1/pub/ContinuationFest-ruby.pdf,其中说Contination已经破坏,而他们Ruby实现的创建者都是犯罪分子.另一方面,我读了一些博客,其中作者表示他们对延续的兴奋,他们并没有对延续错误感到悲伤.所以我不知道该怎么想.我知道使用Fibers而不是Continuations更好,但是当Fibers不够时该怎么办?我可以使用Continuations还是它们坏了(2008年的演示文稿说明了这一点)?为什么Matz踢了stdlib的Continuations?为什么有选票,他会从Ruby中踢出来?
我在http://community.schemewiki.org/?call-with-current-continuation中查看以下关于协程的示例:
(define (hefty-computation do-other-stuff)
(let loop ((n 5))
(display "Hefty computation: ")
(display n)
(newline)
(set! do-other-stuff (call/cc do-other-stuff)) ; point A
(display "Hefty computation (b)")
(newline)
(set! do-other-stuff (call/cc do-other-stuff))
(display "Hefty computation (c)")
(newline)
(set! do-other-stuff (call/cc do-other-stuff))
(if (> n 0)
(loop (- n 1)))))
Run Code Online (Sandbox Code Playgroud)
多余的工作:
;; notionally displays a clock
(define (superfluous-computation do-other-stuff)
(let loop ()
(for-each (lambda (graphic)
(display graphic)
(newline)
(set! do-other-stuff (call/cc do-other-stuff)))
'("Straight up." "Quarter after." "Half past." "Quarter til.")) …Run Code Online (Sandbox Code Playgroud) 这是一个参考问题:StackOverflow在延续monad中
与我玩了一点,需要一些澄清.
1)我想这个:
member this.Delay(mk) = fun c -> mk () c
Run Code Online (Sandbox Code Playgroud)
使得计算工作流程中的行为能够实现这些差异,如toyvo所示:
cBind (map xs) (fun xs -> cReturn (f x :: xs))
cBind (fun c -> map xs c) (fun xs -> cReturn (f x :: xs))
Run Code Online (Sandbox Code Playgroud)
所以我并不完全明白什么是诀窍,什么时候
(fun c -> map xs c)只有不同的符号(map xs)
2)推理问题.- 在OP的第二个映射示例中,我发现它由于推理问题而无法编译v,因为它推断f为a -> b list而不是期望的a -> b.为什么以这种方式推断?如果let v = f x它会很好地推断.
3)在我看来,VS在工具提示中显示不准确的类型签名:monad的返回类型返回为:('e->'f)->f,而Bind的返回类型仅为'c->'b.- 它似乎('e->'f)只c …
我想用这样的类型创建一个自动机类型:
newtype Auto i o = Auto {runAuto :: i -> (o, Auto i o)}
Run Code Online (Sandbox Code Playgroud)
我知道这是Automata 箭头的类型,但我不是在寻找箭头。我想让它成为一个 monad,所以大概它会有一个类型
newtype Auto i o a = ???? What goes here?
Run Code Online (Sandbox Code Playgroud)
具有这样的功能:
yield :: o -> Auto i o i
Run Code Online (Sandbox Code Playgroud)
因此,当我从 Auto monad 中调用“yield”时,“runAuto”函数返回由“yield”的参数和延续函数组成的对。当应用程序调用延续函数时,参数作为“yield”的结果在 monad 中返回。
我知道这将需要一些延续 monad 的风格,但尽管过去曾与延续争论不休,但我不知道如何编写这个代码。
我也知道这很像 Michael Snoyman 的Conduit monad,除了他将“yield”和“await”分开。这个 monad 必须为每个输入只有一个输出。
背景:我正在编写一些以复杂方式响应 GUI 事件的代码。我希望能够编写接受一系列输入的代码,而不是将其变成手动编码的状态机,以换取随着用户交互的进行而对屏幕进行更新。
编辑
这一切都被证明是微妙的错误。我写了 Petr Pudlák 在他的回复中建议的代码,它似乎有效,但“yield”操作总是产生前一个yield的输出。这很奇怪。
在盯着屏幕看了很久之后,我终于发现我需要把代码贴在这里。关键的区别在于 AutoF 类型。将下面的方法与 Petr 提出的方法进行比较。
import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.State.Class …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用任务并行库对树进行求和,其中只生成子任务,直到遍历树直到某个深度,否则它使用延续传递样式对剩余的子节点求和,以避免堆栈溢出.
但是,代码看起来很丑陋 - 使用状态monad来承载当前深度会很好,但状态monad不是尾递归.或者,我如何修改继续monad来携带状态?或者创建状态和延续monad的组合?
let sumTreeParallelDepthCont tree cont =
let rec sumRec tree depth cont =
let newDepth = depth - 1
match tree with
| Leaf(num) -> cont num
| Branch(left, right) ->
if depth <= 0 then
sumTreeContMonad left (fun leftM ->
sumTreeContMonad right (fun rightM ->
cont (leftM + rightM )))
else
let leftTask = Task.Factory.StartNew(fun () ->
let leftResult = ref 0
sumRec left newDepth (fun leftM ->
leftResult := leftM)
!leftResult
)
let rightTask = …Run Code Online (Sandbox Code Playgroud) 显然,MonadConts更受限制,并且比普通的Monads 更强大,这要归功于它callCC.这意味着它的实例越来越少,你可以用它做更多的事情.
当看的定义的实例MonadCont,它看起来像一切所列要求或者Cont或者ContT或者已经存在的MonadCont实例.这意味着我们必须开始与一些Cont或者ContT,特别是不能把IO成MonadCont.
但是,我认为callCC在IO上下文中使用是有意义的,因此我们可以简化以下内容(根据官方Hackage页面 callCC示例进行调整):
whatsYourName :: IO ()
whatsYourName = do
name <- getLine
let response = flip runCont id $ do
callCC $ \exit -> do
when (null name) (exit "You forgot to tell me your name!")
return $ "Welcome, " ++ name ++ "!"
print response
Run Code Online (Sandbox Code Playgroud)
成
whatsYourName' :: …Run Code Online (Sandbox Code Playgroud) 我有一个旧的Fortran 77代码,我想在F90编译器中运行,我的目标是尽可能少地更改代码.它工作得很好,但我在代码中的格式语句有一些问题.我不明白这是什么问题.我使用Eclipse和gfortran.我使用自由形式.
编译好:
program HelloWorld
400 FORMAT(7HK GAMMA,2X,G13.5,7H P0,2X,G13.5,6H A1,2X,G13.5)
end program
Run Code Online (Sandbox Code Playgroud)
这不编译
program HelloWorld
400 FORMAT(7HK 'GAMMA',2X,G13.5,7H 'P0',2X,G13.5,6H 'A1',2X,G13.5)
1
end program
Run Code Online (Sandbox Code Playgroud)
错误是
P描述符需要格式字符串中的前导比例因子(1)
(错误是从德语翻译的,可能不是完全相同的英文单词)有什么问题?
这也编译好:
program HelloWorld
400 FORMAT(7HK GAMMA,2X,G13.5,7H P0, &
2X,G13.5,6H A1,2X,G13.5)
end program
Run Code Online (Sandbox Code Playgroud)
如果我在最后一个代码中添加更多代码:
program HelloWorld
400 FORMAT(7HK GAMMA,2X,G13.5,7H P0,2X,G13.5,6H A1,2X,G13.5, &
2X,7HK,ALPHA-1,2X,G13.5,7H BETA-4,2X,G13.5 )
end program
Run Code Online (Sandbox Code Playgroud)
它不再编译.错误是:
P格式字符串*中预期的编辑描述符(1)
而(1)位于右括号后的第三行.
*我不确定"格式字符串"的翻译,因为我的控制台是德语.
有什么问题?
我不得不在今天的 while 条件中添加无关的括号以避免 pep8 投诉:
while not found and not something and \
(time_left is None or time_left > 0):
(one, two, three, four) = self.gimme(timeout=time_left)
Run Code Online (Sandbox Code Playgroud)
我的解决方案:
while (not found and not something and
(time_left is None or time_left > 0)):
(one, two, three, four) = self.gimme(timeout=time_left)
Run Code Online (Sandbox Code Playgroud)
如果我更改了第 2 行缩进,它会抱怨缩进过多或缩进缺失,对于从 W 中的每个缩进到它右侧的 8。
我很烦恼添加无关的括号来满足 pep8,为了几乎没有提高可读性,违背了一般原则。
有任何想法吗?我错过了更好的解决方案吗?
我有这个代码块我想评论,但内联注释不起作用.我不确定PEP8指南适用于何处.建议吗?
if next_qi < qi + lcs_len \ # If the next qLCS overlaps
and next_ri < ri + lcs_len \ # If the next rLCS start overlaps
and next_ri + lcs_len > ri: # If the next rLCS end overlaps
del candidate_lcs[qi] # Delete dupilicate LCS.
Run Code Online (Sandbox Code Playgroud)