我正在编写一个具有非常复杂的循环的脚本:
main = do
inFH <- openFile "..." ReadMode
outFH <- openFile "..." WriteMode
forM myList $ \ item ->
...
if ...
then ...
else do
...
case ... of
Nothing -> ...
Just x -> do
...
...
Run Code Online (Sandbox Code Playgroud)
代码很快就会飞到右边,所以我想把它分成几块,使用例如where子句.问题是,许多这些...包含读/写语句到两个把手inFH和outFH,使用where的语句将呈现这两个名字断章取义.我每次使用where语句时都必须发送这两个变量.
有没有更好的方法来解决这个问题?
我正在编写一个opengl C程序,我知道大多数图形作业都是由GPU完成的.我的问题是,我可以使用GPU来计算与图形无关的东西吗?例如,计算1 + 2 + 3 + ... + 100 = ?
在Chrome控制台中尝试这两行(当然使用jQuery上下文):
$('<head><title>FooBar</title></head>').find('title')
Run Code Online (Sandbox Code Playgroud)
会屈服[],但是
$('<head><title>FooBar</title></head>').filter('title')
Run Code Online (Sandbox Code Playgroud)
给[<title>?FooBar?</title>?].但在这个例子中,<title> 是后代<head>,所以不应该发现使用find?
有人可以解释为什么不呢?
打印表达式的值是调试中的常见做法.例如,如果我有一段这样的代码
my . super . cool . fUnCtIoN . chain $ value
Run Code Online (Sandbox Code Playgroud)
而且我想看看输出fUnCtIoN . chain,我想补充一下
my . super . cool . (\ x -> traceShow x x ) . fUnCtIoN . chain $ value
Run Code Online (Sandbox Code Playgroud)
对于像这样的简单任务来说,这是满口的,更不用说我是否要打印许多中间结果:
(\ x -> traceShow x x )
. my
. (\ x -> traceShow x x )
. super
. (\ x -> traceShow x x )
. cool
. (\ x -> traceShow x x )
. fUnCtIoN
. (\ x -> …Run Code Online (Sandbox Code Playgroud) 我试图定义一个非常简单的数据结构,假设将Infinity元素添加到任何类型下Num.我还把它放在一个定义的类下面NumContainer,该类有fromNum一个NumWithInf使用常规构造的方法Num.代码非常简单.
data NumWithInf a = Infinity | Finite a deriving Show
class NumContainer k where
fromNum :: Num a => a -> k
instance Num a => NumContainer (NumWithInf a) where
fromNum x = Finite x
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,GHCI给了我以下错误:
hw.hs:7:24:
Could not deduce (a ~ a1)
from the context (Num a)
bound by the instance declaration at hw.hs:6:10-45
or from (Num a1)
bound by the type signature for
fromNum :: …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试parallel包,特别是par功能.我写了一个简单的程序来测试并行性是否会加速顺序程序.
所以在这里我写了两个脚本,第一个顺序:
import Control.Parallel
n = 600000000
main = print $ pseq (mod (sum [1..n]) 5) (mod (sum [1..n]) 5)
Run Code Online (Sandbox Code Playgroud)
第二个并行:
import Control.Parallel
n = 600000000
main = print $ par (mod (sum [1..n]) 5) (mod (sum [1..n]) 5)
Run Code Online (Sandbox Code Playgroud)
我使用ghc -O2并运行它们编译每个,然后使用GNU测量运行时间time.这是我得到的:
顺序:
User time (seconds): 13.79
System time (seconds): 0.04
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:13.85
Run Code Online (Sandbox Code Playgroud)
平行:
User time (seconds): 6.89
System time …Run Code Online (Sandbox Code Playgroud) 我知道在Haskell中有一个非常有用的简洁模式,用于将函数应用于参数列表:
?> rem <$> [23, 45] <*> [7, 11]
[2,1,3,1]
Run Code Online (Sandbox Code Playgroud)
是否有一种类似的方便方法只能调用相同的相应索引中的项目?在上面的例子中,它只是rem 23 7而rem 45 11不是所有可能性.
纵观源代码fail的MaybeT实例Monad:
instance (Monad m) => Monad (MaybeT m) where
fail _ = MaybeT (return Nothing)
Run Code Online (Sandbox Code Playgroud)
很明显,fail没有使用论证.那为什么没有更清洁的功能,这样quit :: MaybeT m ()可以节省我们fail ""每次打字的时间?或者我错过了什么?
说我有一个字典TSV文件dict.txt:
apple pomme
umbrella parapluie
glass verre
... ...
Run Code Online (Sandbox Code Playgroud)
和另一个list.txt包含单词列表的文件(从左栏开始dict.txt):
pie
apple
blue
...
Run Code Online (Sandbox Code Playgroud)
我想将它们翻译成右栏中的相应单词dict.txt,即:
tarte
pomme
bleu
...
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?
有时我需要使用几个嵌套MonadTrans.例如,我会在一个MaybeT内部ExceptT,模仿continue和break命令式编程:
runExceptT . forM [1..] $ \ _ -> runMaybeT $ do
...
mzero -- this mimics continue
lift $ throwE "..." -- this mimics break
lift . lift $ putStrLn "Hello!"
...
Run Code Online (Sandbox Code Playgroud)
但是,正如上面的代码所示,每次我需要IO在这个"人工循环"中进行任何操作时,我需要lift . lift在它之前放一个丑陋的东西.想象一下,如果我有更复杂的嵌套和大量IO操作,这很快就会成为一种厌恶.我如何使代码更清晰,更简洁?