这是关于Haskell中的语法糖.一个简单的Haskell程序:
main = do
args <- getArgs
let first = head args
print first
Run Code Online (Sandbox Code Playgroud)
我在第一行(args <- getArgs
)中使用绑定,在第二行()中使用纯赋值let first = ...
.有可能将它们合并成一个可读的单行程序吗?
我明白我可以重写绑定"脱糖":
main = do
first <- getArgs >>= ( return . head )
print first
Run Code Online (Sandbox Code Playgroud)
但是有没有更好的方法,没有用(>> =)弄乱线路并返回?
import numpy as np
a = np.arange(1000000).reshape(1000,1000)
print(a**2)
Run Code Online (Sandbox Code Playgroud)
有了这段代码,我得到了这个答案.为什么我会得到负值?
[[ 0 1 4 ..., 994009 996004 998001]
[ 1000000 1002001 1004004 ..., 3988009 3992004 3996001]
[ 4000000 4004001 4008004 ..., 8982009 8988004 8994001]
...,
[1871554624 1873548625 1875542628 ..., -434400663 -432404668 -430408671]
[-428412672 -426416671 -424420668 ..., 1562593337 1564591332 1566589329]
[1568587328 1570585329 1572583332 ..., -733379959 -731379964 -729379967]]
Run Code Online (Sandbox Code Playgroud) 我想写一个安全的版本toEnum
:
safeToEnum :: (Enum t, Bounded t) => Int -> Maybe t
Run Code Online (Sandbox Code Playgroud)
一个天真的实现:
safeToEnum :: (Enum t, Bounded t) => Int -> Maybe t
safeToEnum i =
if (i >= fromEnum (minBound :: t)) && (i <= fromEnum (maxBound :: t))
then Just . toEnum $ i
else Nothing
main = do
print $ (safeToEnum 1 :: Maybe Bool)
print $ (safeToEnum 2 :: Maybe Bool)
Run Code Online (Sandbox Code Playgroud)
它不起作用:
safeToEnum.hs:3:21:
Could not deduce (Bounded t1) from the context ()
arising …
Run Code Online (Sandbox Code Playgroud) 一方面,在Haskell中Vector a
似乎是用作数字数组的首选类型.甚至有一个(不完整的)矢量教程.
另一方面,Control.Parallel.Strategies
主要是根据而定义的Traversable
.矢量库不提供这些实例.
最小的完整定义也Traversable t
应该定义Foldable
和
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
sequenceA :: Applicative f => t (f a) -> f (t a)
Run Code Online (Sandbox Code Playgroud)
我看不出如何sequenceA
定义Data.Vector.Unboxed.Vector
.那么,使用未装箱的向量编写并行代码的最佳方法是什么?定义一些新的临时策略,如evalVector
或使用par
和pseq
明确或使用普通Data.Array
而不是向量?
PS Plain Array
可并行化,没有问题:https://gist.github.com/701888
有人可以解释我这个错误:
user> (let [^int i 3] i)
CompilerException java.lang.UnsupportedOperationException: Can't type hint a local with a primitive initializer, compiling:(NO_SOURCE_PATH:1)
Run Code Online (Sandbox Code Playgroud)
我不明白,
到底是什么我不能打字 - 为什么?
为什么我可以在相同的情况下使用数组类型提示?
user> (let [^ints ii (int-array 1)] ii)
#<int[] [I@334a123f>
Run Code Online (Sandbox Code Playgroud)如何键入提示本地整数变量?
对不起,我找不到这个问题的最佳标题.以下是我的要求.
我正在研究一个项目,该项目处理不同账户的大量资金交易.系统自动将资金转账到A,B,C等账户,但在此之前,A,B或C中的某个人应该(电子地)批准转账金额.
你认为最好的方法是什么?我希望系统发送一个数字签名的文件(某物的PDF)(?),A,B和C的授权人员应检查并确认数量是否正确.
由于数量很高,我必须确保系统发出的文件没有被篡改,同时我也想确保A,B或C发送的文件(回复)也没有被篡改.实现它的最佳方法是什么?有任何想法吗?
是否可以使用类型同义词作为monad变换器类型构造函数的参数?特别是,如果应用的monad变换器有一元类型的同义词,它是否可以用作另一个monad变换器中底层monad的类型?
从我看到的类型同义词不被接受为第一类类型构造函数,请参阅下面的示例和错误消息:
-- Using type synonym of a monad transformer in another monad transformer.
import Control.Monad.Reader
-- inner transformer
type A a = ReaderT Int IO a
-- type B a = ReaderT String A a
{- Error:
readert2.hs:8:0:
Type synonym `A' should have 1 argument, but has been given 0
In the type synonym declaration for `B'
-}
-- type B a = ReaderT String (A a) a
{- Error:
readert2.hs:15:27:
Kind mis-match
The second argument of `ReaderT' should …
Run Code Online (Sandbox Code Playgroud) 我使用mercurial.el
Emacs模式.当我运行时vc-diff
,我可以看到差异,但是,与源代码不同,它没有很好地突出显示:
阅读这些差异很困难.我如何配置Emacs,
-
和+
不同颜色的线条?(例如红色和蓝色)我们假设有两个分支,master
并且slave
,它们编辑同一个文件和同一行.最初,文件的内容是
foo bar
Run Code Online (Sandbox Code Playgroud)
然后在分支slave
中编辑成为
foo bar baz
Run Code Online (Sandbox Code Playgroud)
现在,分支的用户slave
格式化patch(git format-patch master
)并将其发送给分支的用户master
.同时,在分支master
中编辑相同的文件并成为
foo bar spam eggs
Run Code Online (Sandbox Code Playgroud)
无法应用修补程序,并master
要求slave
合并并创建新修补程序.何时master
合并slave
并解决冲突,是时候重新格式化补丁.提交图如下所示:
slave: master:
foo bar baz spam eggs
| \
| \
| foo bar spam eggs
| |
foo bar baz |
\ |
\ |
+--------- foo bar
Run Code Online (Sandbox Code Playgroud)
最新的提交(合并)slave
看起来像:
@@@ -1,1 -1,1 +1,1 @@@
- foo bar baz
-foo bar …
Run Code Online (Sandbox Code Playgroud) 我的项目中有一个虚拟模块,其唯一目的是为库的其余部分保存Haddock文档.其实我并不需要这个模块中输入任何东西,但如果我不导入其他模块,黑线鳕不会将超级链接功能名称及其模块.
我的模块看起来像这样
{- |
Lots of Haddock text here... it references 'someFunction'.
-}
module TopLevelDoc () where
import Other.Module.With.SomeFunction
Run Code Online (Sandbox Code Playgroud)
现在,如果我构建项目,我会收到此警告:
Warning: The import of `Other.Module.With.SomeFunction' is redundant
except perhaps to import instances from `Other.Module.With.SomeFunction'
To import instances alone, use: import Other.Module.With.SomeFunction()
Run Code Online (Sandbox Code Playgroud)
如果我删除导入或制作它们()
,Haddock不会超链接someFunction
到其文档.如果我按原样留下这样的进口,我会收到许多我不喜欢的错误警告.而且我不想为整个项目抑制这种警告,它可能对任何其他模块都有用,但是这个.
问题:
ghc-options
的.cabal
)haskell ×5
clojure ×1
cryptography ×1
diff ×1
emacs ×1
format-patch ×1
git ×1
haddock ×1
merge ×1
numpy ×1
polymorphism ×1
python ×1
syntax ×1
type-hinting ×1
types ×1
vector ×1