小编PyR*_*lez的帖子

当变量引用同一个对象时,如何调用它,为什么python具有此功能?

在python中,两个不同的变量可以表示同一个对象.注意:

>>> list1=['This is list1.']
>>> list2=list1
>>> list2[0] = 'This is actually list2 not list one.'
>>> print list1
['This is actually list2 not list one.']
Run Code Online (Sandbox Code Playgroud)

这是此代码的链接. 如您所见,没有list1或list2,只有一个列表有两个名称.我很清楚这种效果,我从一本书中记得这是故意的,但我忘记了这种现象.此外,它偶尔是bug的来源,而其他语言没有这个问题.我确实感觉某些与物体有关的东西会在没有物体的情况下破裂.这有什么特别的好处(因为python是我真正知道的.)

python reference pass-by-reference python-2.7

3
推荐指数
1
解决办法
117
查看次数

让Haskell在编译时扩展某些thunk?

有没有办法让Haskell在运行时扩展某些thunk.例如,说我有

--Purposely inefficient code for demonstration
fib 0=0
fib 1=1
fib n=fib n=fib (n-1) + fib (n-2)
goldRatio=fib 100 / fib 101
Run Code Online (Sandbox Code Playgroud)

我怎么能goldRatio在编译时评估它.例如,与

{-# EVALUATE goldRatio #-}
Run Code Online (Sandbox Code Playgroud)

它只需要弱头形状,因为Control.Deepseq.force可以处理其余部分.我听说模板haskell可以做到这一点,但我不太清楚.

注意:我现在正在使用GHC.

evaluation haskell compilation thunk

3
推荐指数
1
解决办法
160
查看次数

项目Euler#14在Haskell中的提示?

我正在尝试euler挑战14.我想知道我是否可以在haskell中快速计算它.我试过这种天真的方法.

import Data.List import Data.Function collatz n | even n = n quot 2 | otherwise = 3*n+1 colSeq = takeWhile (/= 1) . (iterate collatz) main=print $ maximumBy (compare on (length . colSeq)) [1..999999]

但这花了太长时间.

? <*Main System.Timeout>: timeout (10^6*60) main
Nothing
Run Code Online (Sandbox Code Playgroud)

我也尝试使用反向collat​​z关系,并在地图中保留长度以消除冗余计算,但这也不起作用.并且不想要解决方案,但是有没有人有一些数学文献或编程技术可以使这更快,或者我只需要让它过夜?

math optimization haskell

2
推荐指数
1
解决办法
256
查看次数

如何安装`帽子`

我试图获得Hat调试器.当我尝试:

cabal install hat 要么 cabal install hat -v

最后我得到:

configure: error: in `/tmp/terminfo-0.4.0.0-17745/terminfo-0.4.0.0':
configure: error: curses headers could not be found, so this package cannot be built
See `config.log' for more details
Failed to install terminfo-0.4.0.0
cabal: Error: some packages failed to install:
haskeline-0.7.1.3 depends on terminfo-0.4.0.0 which failed to install.
hat-2.8.0.0 depends on terminfo-0.4.0.0 which failed to install.
terminfo-0.4.0.0 failed during the configure step. The exception was:
ExitFailure 1
Run Code Online (Sandbox Code Playgroud)

所以麻烦我尝试:

cabal install terminfo
Run Code Online (Sandbox Code Playgroud)

得到:

configure: error: …
Run Code Online (Sandbox Code Playgroud)

debugging haskell hat cabal cabal-install

2
推荐指数
1
解决办法
709
查看次数

我试图打破Haskell并从GHC得到一个"无法访问的代码"错误.这是什么意思?

所以,我有以下代码:

{-# LANGUAGE GADTs #-}

import Data.Coerce
import Data.Functor.Fixedpoint --Although I'm not using these yet, they provide "context"

data Refl a b where
    Refl :: Refl a a

weird :: Refl a [a] -> a
weird Refl = [[], [[], []]]
Run Code Online (Sandbox Code Playgroud)

我想你可以看到我要去的地方.如果没有,我想要做的就是强迫Haskell思考a并且[a]通过赋予它Refl参数是相同的类型.这将允许我做恶作剧.当我编译它时,ghci给我这个错误:

[1 of 1] Compiling Main             ( pad'.hs, interpreted )

pad'.hs:9:7:
    Couldn't match type ‘a’ with ‘[a]’
      ‘a’ is a rigid type variable bound by
          the type signature for weird :: …
Run Code Online (Sandbox Code Playgroud)

haskell types compiler-errors dependent-type

2
推荐指数
1
解决办法
135
查看次数

为什么不存在量化和数据合作?

{-# LANGUAGE DataKinds, ExistentialQuantification, KindSignatures #-}
import Data.Proxy

data Type t= forall (a :: t). Type (Proxy a)
Run Code Online (Sandbox Code Playgroud)

给出了错误

Type variable ‘t’ used in a kind
In the kind ‘t’
In the definition of data constructor ‘Type’
In the data declaration for ‘Type’
Run Code Online (Sandbox Code Playgroud)

但是t是一个Kind变量,而不是一个类型变量.这是怎么回事?

haskell existential-type ghc dependent-type data-kinds

2
推荐指数
1
解决办法
201
查看次数

GHC如何在运行时代表`undefined :: Void`?

Void除此之外,该类型没有值undefined.GHC如何undefined :: Void在运行时代表?

null haskell runtime ghc

1
推荐指数
1
解决办法
127
查看次数

Haskell多线互动模式

在python中,有许多IDE可以让你这样做.

>>> if (a==5):
      print "Yes"
else:
      print "No"
Run Code Online (Sandbox Code Playgroud)

但是在haskell的GHCi中:

Prelude> do

<interactive>:2:1: Empty 'do' block
Run Code Online (Sandbox Code Playgroud)

这是行不通的.是否有任何IDE允许您以交互模式执行多行命令.

注意:我知道:{和:}命令,但您输入后无法编辑上面的行.

ide haskell ghci

0
推荐指数
1
解决办法
181
查看次数