1

I am totally newbie to haskell.

I have such snippet code

lucky:: Int->String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry , youre out of luck pal!" 

I tried to input to terminal directly, seems not right. But if I want to put this in file and load this file, then call lucky function. How should I construct this file? Thank you!

I tried this:

module Main where
lucky:: Int->String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry , youre out of luck pal!" 


main = do 

But when i try to call lucky in terminal, i got this:

factorial.hs:7:8: Empty 'do' construct
lucky 7

<interactive>:1:1: Not in scope: `lucky'
4

2 に答える 2

5

問題は、最後の行main = doが構文エラーであることです。ファイルに構文エラーがある場合、ファイル内の関数はロードされません。main の定義を取り出してロードしようとすると、正常に動作するはずです。

余談ですが、一般的に Haskell 型のシグネチャは のようにスペースを空けて書かれlucky :: Int -> Stringます。

于 2012-04-29T00:42:50.653 に答える
4

module Main whereまたはmain = do行は必要ありません。

「.hs」拡張子を付けてファイルを保存し、ファイル名を入力するだけで、関数定義を ghci にロードして使用でき:loadます。

于 2012-04-29T01:39:19.267 に答える