2017-01-24 33 views
1

經典問題,但在Haskell這次。我嘗試這樣做的C方式,但我不斷收到此錯誤Haskell中整數的位數

Variable not in scope: numDigits :: Integer -> t 

下面是我寫的

numDigits :: Integer -> Integer 
numDigits n = toInteger (round (logBase 10 (fromIntegral n)) + 1) 

代碼難道我錯了嗎?或者日誌

+0

什麼'toInteger'的簽名?不記得了。另外,爲什麼不把它定義爲'長度。 show'? – Carcigenicate

+0

toInteger,因爲日誌庫10返回一個雙我想? –

+0

你是在REPL還是源文件中執行此操作? – Carcigenicate

回答

1

僅供參考我投票結束是因爲您有輸入錯誤或未能向我們提供實際失敗的代碼。有問題的代碼是有效的,但您應該指定如何加載它以及如何調用它。

出於演示:

% cat so.hs 
numDigits :: Integer -> Integer 
numDigits n = toInteger (round (logBase 10 (fromIntegral n)) + 1) 
% ghci so.hs 
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help 
Loaded GHCi configuration from /Users/tommd/.ghci 
[1 of 1] Compiling Main    (so.hs, interpreted) 
Ok, modules loaded: Main. 
*Main> numDigits 1948 
4