我有一个函数,它接受一个参数并产生一个结果.不幸的是,函数产生结果需要很长时间.使用相同的输入经常调用该函数,这就是为什么我可以方便地缓存结果.就像是
let cachedFunction = createCache slowFunction
in (cachedFunction 3.1) + (cachedFunction 4.2) + (cachedFunction 3.1)
Run Code Online (Sandbox Code Playgroud)
我正在研究Data.Array,虽然数组是懒惰的,但我需要用一对对象(使用listArray)初始化它 - 这是不切实际的.如果'key'是例如'Double'类型,我根本无法初始化它,即使理论上我可以为每个可能的输入分配一个Integer,我有几万个可能的输入,我实际上只使用了少数几个.我需要使用函数而不是列表初始化数组(或者,最好是哈希表,因为只使用少量的resutls).
更新:我正在阅读备忘录文章,据我所知,MemoTrie可以按我想要的方式工作.也许.有人可以尝试生成'cachedFunction'吗?对于一个需要2个Double参数的慢函数?或者,或者,在〜[0..1亿]的域中采用一个不会占用所有内存的Int参数?
鉴于该计划:
import Language.Haskell.Exts.Annotated -- from haskell-src-exts
import System.Mem
import System.IO
import Control.Exception
main :: IO ()
main = do
evaluate $ length $ show $ fromParseResult $ parseFileContents $ "data C = C {a :: F {- " ++ replicate 400000 'd' ++ " -} }"
performGC
performGC
performGC
Run Code Online (Sandbox Code Playgroud)
使用GHC 7.0.3,当我运行时:
$ ghc --make Temp.hs -rtsopts && Temp.exe +RTS -G1 -S
Alloc Copied Live GC GC TOT TOT Page Flts
bytes bytes bytes user elap user elap
...
29463264 64 …
Run Code Online (Sandbox Code Playgroud) 我正在将一些Haskell代码从使用列表更改为集合.我认为,我理解所需的一切,但我不确定如何在套装上进行模式匹配.列表有这个很好的文字语法,似乎很难用Set构造函数模拟.例如,我可能有一些像这样的代码:
foo [] = []
foo x = other_thing
Run Code Online (Sandbox Code Playgroud)
如何编写此代码,以便使用集而不是列表?
在阅读Wouter Swierstra撰写的文章"数据类型单点"时,我一直坚持将以下Haskell代码翻译成Scala:
data Expr f = In (f (Expr f ))
Run Code Online (Sandbox Code Playgroud)
Expr
是用于表示算术表达式的数据类型,具体表达式的编写方式如下:
data Val e = Val Int
type IntExpr = Expr Val
data Add e = Add e e
type AddExpr = Expr Add
Run Code Online (Sandbox Code Playgroud)
我的问题是在Scala 中实现f
(可能被认为是构造函数的签名).
PS定义两个签名的副产品,稍后您可以组合数据类型,获得类型的表达式Expr (Val :+: Add )
:
data (f :+: g) e = Inl (f e) | Inr (g e)
addExample :: Expr (Val :+: Add )
addExample = In (Inr (Add (In …
Run Code Online (Sandbox Code Playgroud) 如何在Objective-C中创建一个字节数组?
我有一些这样的数据:0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89
,我想准备一个字节数组.
我只是对Haskell中模式匹配的效率感到好奇.什么是模式匹配优于嵌套if
/ case
语句然后相反的简单情况?
谢谢你的帮助.
import Control.Applicative
import Control.Arrow
filter ((&&) <$> (>2) <*> (<7)) [1..10]
filter ((>2) &&& (<7) >>> uncurry (&&)) [1..10]
Run Code Online (Sandbox Code Playgroud)
两者都得到相同的结果!但是,我很难理解.有人可以在这里详细解释一下吗?
我正在尝试使用指定为的算法实现unify函数
unify ? ? = idSubst
unify ? ? = update (?, ?) idSubst
unify ? (?1 ? ?2) =
if ? ? vars(?1 ? ?2) then
error ”Occurs check failure”
else
update (?, ?1 ? ?2) idSubst
unify (?1 ? ?2) ? = unify ? (?1 ? ?2)
unify (?1 ?1 ?2) (?3 ?2 ?4) = if ?1 == ?2 then
(subst s2) . s1
else
error ”not uni?able.”
where s1 = unify ?1 ?3
s2 = unify (subst …
Run Code Online (Sandbox Code Playgroud) 因此,我只是写了一个小片段来生成Mandelbrot分形,并想象我的惊喜,当它出来时都是丑陋和倾斜的(正如你在底部看到的那样).我很欣赏为什么会发生这种情况的方向.这是一次学习经历,我不是在寻找任何人为我做这件事,但我有点在调试它.违规代码是:
module Mandelbrot where
import Complex
import Image
main = writeFile "mb.ppm" $ imageMB 1000
mandelbrotPixel x y = mb (x:+y) (0:+0) 0
mb c x iter | magnitude x > 2 = iter
| iter >= 255 = 255
| otherwise = mb c (c+q^2) (iter+1)
where q = x -- Mandelbrot
-- q = (abs.realPart $ x) :+ (abs.imagPart $ x) --Burning Ship
argandPlane x0 x1 y0 y1 width height = [ (x,y) |
y <- [y1, y1 …
Run Code Online (Sandbox Code Playgroud) 我正在阅读一个使用以下示例的教程(我将稍微概括一下):
f :: Foo -> (Int, Foo)
...
fList :: Foo -> [Int]
fList foo = x : fList bar
where
(x, bar) = f foo
Run Code Online (Sandbox Code Playgroud)
我的问题在于,似乎你可以参考,x
并且bar
,通过名称,在获得它们的元组之外.如果我的猜测是正确的,这似乎就像在其他语言中解构参数列表一样.(换句话说,我没有做以下:)
fList foo = (fst tuple) : fList (snd tuple)
where
tuple = f foo
Run Code Online (Sandbox Code Playgroud)
我对这种行为是对的吗?在我读过的教程/书籍中,我从未见过它.有人能指出我有关这个问题的更多信息吗?
编辑:任何事情(列表,数组等)都可以以类似的方式进行解构,或者你只能用元组做到这一点吗?
haskell ×9
arrays ×1
byte ×1
caching ×1
ghc ×1
image ×1
mandelbrot ×1
memoization ×1
objective-c ×1
scala ×1
syntax ×1
types ×1
unify ×1