2017-02-08 26 views
4

我今天就開始在哈斯克爾,並且我在ghci上執行的所有功能都顯示此消息。我只是想知道這是爲什麼happenning.I知道,存在很多關於這個問題,但是這是一個簡單的例子,我需要知道這個錯誤在開始時錯誤haskell:不在範圍內。這是什麼意思?

function3 :: Int -> [Int] 
function3 x = [a | a <- [1..x] mod a x == 0] 
+4

「不在範圍內」意味着您正在嘗試使用未在您嘗試使用它的地方定義的名稱。在這種情況下,發生這種情況的原因是你在'[1..x]'後面留下了一個逗號,所以你在列表理解中的'a'定義不能正常工作。將其更改爲'[a | a < - [1..x],mod a x == 0]' – duplode

+1

如果在問題中包含整個GHCi錯誤消息,它會有所幫助。 – wizzup

回答

5

,當你鍵入錯誤沒有發生GHCi中的函數類型?

$ ghci 
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help 
Prelude> function3 :: Int -> [Int] 

<interactive>:1:1: error: 
    Variable not in scope: function3 :: Int -> [Int] 
Prelude> 

如果是的話,你必須使用多行輸入

Prelude> :{ 
Prelude| function3 :: Int -> [Int] 
Prelude| function3 x = [a | a <- [1..x], mod a x == 0] 
Prelude| :} 

而之前mod

指出,另外,爲了更好的工作流程,您可以將您的密碼保存到一個文件並使用GHCi負載:load

$ cat tmp/functions.hs 
function3 :: Int -> [Int] 
function3 x = [a | a <- [1..x], mod a x == 0] 

$ ghci 
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help 
Prelude> :l tmp/functions.hs 
[1 of 1] Compiling Main    (tmp/functions.hs, interpreted) 
Ok, modules loaded: Main. 
*Main> :t function3 
function3 :: Int -> [Int] 
*Main>