簡體   English   中英

第一個代碼示例中的“Get Programming With Haskell”錯誤

[英]"Get Programming With Haskell" error in the first code example

我正在閱讀“Get Programming With Haskell”,第二課是編寫和編譯這個簡單的代碼

toPart rec = "Dear " ++ rec ++ " \n"
bodyPart bookT = "Thanks for buying " ++ bookT ++" \n"
fromPart auth = "thanks \n " ++ auth


createEmail rec bookT auth
= toPart rec ++

bodyPart bookT ++

fromPart auth

但我從 GHCI 得到這個錯誤

Prelude> :l first_prog.hs 
[1 of 1] Compiling Main             ( first_prog.hs, interpreted )

first_prog.hs:7:5: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5'
  |
7 | let = toPart rec ++
  |     ^
Failed, no modules loaded.

作者還沒有解釋任何關於語法的事情,所以我不知道這里的錯誤在哪里

錯誤使用空格縮進會導致 Haskell 出現問題。 在代碼中,您可以嘗試將其寫成一行,以免被縮進混淆。 例子 :

createEmail rec bookT auth = toPart rec ++ bodyPart bookT ++ fromPart auth

如果你想使用教程的風格,你需要做的是:

createEmail rec bookT auth
 = toPart rec ++

 bodyPart bookT ++

 fromPart auth

基本上,在每行前面放一個空格,它應該可以解決問題。

Haskell 依賴於縮進,所以我們應該稍微修正一下:

toPart rec = "Dear " ++ rec ++ " \n"
bodyPart bookT = "Thanks for buying " ++ bookT ++" \n"
fromPart auth = "thanks \n " ++ auth   

createEmail rec bookT auth
  = toPart rec ++
    bodyPart bookT ++
    fromPart auth
toPart recepient = "Dear " ++ recepient ++ " \n"
bodyPart title = "Thanks for buying " ++ title ++" \n"
fromPart author = "thanks \n " ++ author   

createEmail recepient title author
  = toPart recepient ++
    bodyPart title ++
    fromPart author

main = do
print "who is the email for?"
recepient <- getLine
print "what is the title?"
title <- getLine
print "Who is the author?"
author <- getLine
print (createEmail recepient title author)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM