我昨天刚刚升级到Mac OSX El Capitan,发现我在终端上运行Haskell的方法不再适用了.
我曾经在终端上运行Haskell cd (the path where my .hs file is in)
,然后键入,然后输入ghci
,最后输入:l (.hs file name)
但它-bash: ghci: command not found
在打字时告诉我ghci
.
那我怎么能在El Capitan上运行Haskell呢?我不熟悉计算机,我只是偶尔学习一些算法以获得乐趣,所以请避免使用技术术语并给我一些明确的步骤.
(我不知道它有没有帮助,我发现这个链接,它表明没有必要重新安装整个平台,但我不知道如何下载并运行它从github附加的可执行文件.)
为什么这样的事情在Haskell中运行得非常慢?
test = [x|a<-[1..100],b<-[1..100],c<-[1..100],d<-[1..100],let x = a]
print $ length test
Run Code Online (Sandbox Code Playgroud)
只有大约10^8
数字可以运行,它应该在眨眼间完成,但它似乎永远运行,几乎崩溃.
我正在使用Python 2.7并编写了以下内容:
def算术(A): x = 1 “” 一些评论在这里 “” 如果x = 1: x = 1 elif x = 2: x = 2 返回0
但是它有缩进的问题:
如果x = 1: ^ IndentationError:意外缩进
那么如何在函数中写注释呢?
(product (take 9 [1000,999..])) `div` (product (map (\j-> product [1..j]) (map (\j->length(j)) (group [2,2,2,2,2,2,2,2,2]))))
Run Code Online (Sandbox Code Playgroud)
上面的代码格式为X div
Y,带X=product (take 9 [1000,999..])
&Y=(product (map (\j-> product [1..j]) (map (\j->length(j)) (group [2,2,2,2,2,2,2,2,2]))))
如果我将代码复制并粘贴到ghci中,它会给我-12740133310672
一个答案,但如果我X & Y
单独计算,我会得到964541486381834014456320000 & 362880
,然后将它们分开给我2658017764500203964000
作为答案.
我认为这种不连贯可能是因为数字太大,但由于计算机可以单独正确地计算X和Y,为什么它不能将它们组合起来呢?
键入时,以下代码不起作用test 10
:
test m = if m `mod` 2==0
then m/2
else m
Run Code Online (Sandbox Code Playgroud)
它说如下:
No instance for (Fractional a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Integral a => Fractional (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
instance Fractional Double -- Defined in ‘GHC.Float’
instance Fractional Float -- Defined in ‘GHC.Float’
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: …
Run Code Online (Sandbox Code Playgroud) A = ['1']
C = A.append('1')
打印C
为什么上面的代码在Python中返回None而不是['1','1']?
place n x
意味着n
在列表中找到整数的位置x
,例如place 2 [1,2,3]
将返回1
:
place :: Int -> [a] -> Int
place n x = length $ takeWhile (/=n) x
Run Code Online (Sandbox Code Playgroud)
但它得到了错误 Couldn't match type ‘a’ with ‘Int’
为什么?takeWhile
应该返回一个列表,它的长度是一个整数,因此place
应该Int
最终输出.