鉴于以下代码,为什么Haskell只在到达第二个时报告错误id
?
data TypeX = TypeX {
id :: Int -- why not the error here?
, val :: String
} deriving (Show)
var1 = TypeX 1 "bananas"
var2 = TypeX {
val = "oranges"
, id = 2 -- why an error here?
}
Run Code Online (Sandbox Code Playgroud)
错误是:
ghci> :l TypeX.lhs
[1 of 1] Compiling Main ( TypeX.lhs, interpreted )
TypeX.lhs:8:13:
Ambiguous occurrence ‘id’
It could refer to either ‘Main.id’, defined at TypeX.lhs:2:15
or ‘Prelude.id’,
imported from ‘Prelude’ at TypeX.lhs:1:1
(and …
Run Code Online (Sandbox Code Playgroud) 我最近开始使用Julia,我发现了bits
函数,它返回其数字参数的位串表示.例如:
julia> bits(1.0)
"0011111111110000000000000000000000000000000000000000000000000000"
Run Code Online (Sandbox Code Playgroud)
但是,在使用此功能时,我惊讶地发现bits
返回非常不同的位串1.0
和2.0
:
julia> bits(1.0)
"0011111111110000000000000000000000000000000000000000000000000000"
julia> bits(2.0)
"0100000000000000000000000000000000000000000000000000000000000000"
Run Code Online (Sandbox Code Playgroud)
我原以为这两个值是相似的......
那些位是什么意思?我含糊地回忆起有关编码指数的比特(来自我的数值分析类),但我真的不记得了,我没有设法在网上找到一个好的描述......
我有一个向量v
的大小1 x 5
,以及对角矩阵D
大小5 x 5
.在我的例子中,我有v = [0 1 2 3 4].
第一个问题:我希望把矢量V的对角线上的D
,所以D(1,1) = 0
,D(2,2) = 1
,D(3,3) = 2
,D(4,4) = 3
和D(5,5) = 4
.
我为此编写了一个matlab代码,但我确信还有另一种自动方法在计算上要便宜得多.所以我写的(并从中进行优化)如下:
ii = 1;
for a = 1 : size(D,1)
for b = 1 : size(D,2)
if(a == b)
D(a,b) = v(1, ii);
ii = ii + 1;
end
end
end
Run Code Online (Sandbox Code Playgroud)
第二个问题:在完成第一个问题之后,我现在需要检查D的对角线值是否等于零.如果我能找到(以自动方式)D对角线上的值等于零,那么将其替换为0.001.
事实上,这可以用很多方式编写,例如:
for a …
Run Code Online (Sandbox Code Playgroud) 如果我编译并运行以下类(使用Java SE 7,如果这很重要),
class Foo {
public static void main(String[] args) {
System.out.println(true ? null : 42);
// System.out.println(null);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
null
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但是,如果我取消注释第二个语句main
,我会收到编译错误:
Foo.java:5: error: reference to println is ambiguous, both method println(char[]) in PrintStream and method println(String) in PrintStream match
System.out.println(null);
^
Run Code Online (Sandbox Code Playgroud)
如果参数System.out.println
是null
,那么为什么Java编译器会抛出此错误
,但如果参数是true ? null : 42
?
我有一个非常具体的awk问题:
在此命令上,输出是带有";"的普通文件 字段分隔符.
awk -F\: '{ FS = ";";}{ if($1>=11414836 && $1<=11500000) print $0;}' /file.txt>/newfile.txt
Run Code Online (Sandbox Code Playgroud)
现在,我需要添加第三个条件:
awk -F\: '{ FS = ";";}{ if($1>=11414836 && $1<=11500000 && $14=3294) print $0;}' /file.txt>/newfile.txt
Run Code Online (Sandbox Code Playgroud)
问题是第三个条件导致输出中的字段分隔符更改为正常空格("").
我究竟做错了什么?
我理解如何设置git以使用.gitignore文件来忽略文件但是想知道为什么这不是一个命令功能,使得这个任务稍微不那么乏味.我稍后会将这个问题发布到git-scm邮件列表,但是想知道这里是否有人可能有正当理由缺少...
'git ignore <pattern>'
Run Code Online (Sandbox Code Playgroud)
......特色.我相信还有其他开发人员会觉得这个功能很有用.
这是一个传递函数:
S = [tf([10 2 4],[1 95 2000 3450])];
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到real(S)
和Imag(S)
?
我想拿一个字符串然后变成一个列表Direction
.例如,"UDDUD"
应该返回[U,D,D,U,D]
,而任何不包含U
或D
返回的字符串Nothing
(例如"UDYD"返回Nothing
).
data Direction = U | D
deriving (Show, Eq)
-- where U is Up and D is Down
findDirection :: [Char] -> Maybe [Direction]
findDirection [] = Nothing
findDirection ['U'] = Just [U]
findDirection ['D'] = Just [D]
findDirection (x:xs)
| x == 'U' = Just (U : findDirection xs)
| x == 'D' = Just (D : findDirection xs)
| otherwise = Nothing
Run Code Online (Sandbox Code Playgroud)
我收到以下错误: …
我想使用Git + Cygwin + Ssh从Github克隆我自己的存储库.
怎么做 - 我创建密钥,我在github.com上发布公钥,我做ssh-add?
我做错了什么?
$git clone git@github.com:ChameleonRed/test.git Cloning into 'test'... Warning: Permanently added the RSA host key for IP address '192.30.252.123' to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
记录更多细节:
OpenSSH_7.2p2, OpenSSL 1.0.2g 1 Mar 2016 debug1: Reading configuration data /etc/ssh_config debug1: Connecting to github.com [192.30.252.123] port 22. debug1: Connection established. debug1: identity file …
A型:
val even1 = (x:Int) => { println("in even1"); x % 2 == 0}
Run Code Online (Sandbox Code Playgroud)
REPL上的输出:
even1: Int => Boolean = <function1>
Run Code Online (Sandbox Code Playgroud)
B型:
val even2 : Int => Boolean = { println("in even2");_% 2 == 0}
Run Code Online (Sandbox Code Playgroud)
REPL上的输出:
in even2
even2: Int => Boolean = <function1>
Run Code Online (Sandbox Code Playgroud)
我知道函数的'def'和'val'声明之间的区别,上面的条件对我来说有点不同.请帮我理解这个区别......