2017-09-08 35 views
-1

我正在尝试从this tutorial的程序。该方案是这样的:有人可以告诉我输入应该如何去?

type Name = String 
type PriceInCents = Int 
type ShoppingListItem = (Name, PriceInCents) 
type ShoppingList = [ShoppingListItem] 

shoppingList :: ShoppingList 
shoppingList = [ ("Bananas", 300) 
       , ("Chocolate", 250) 
       , ("Milk", 300) 
       , ("Apples", 450) 
       ] 

sumShoppingList :: ShoppingList -> PriceInCents 
sumShoppingList []  = 0 
sumShoppingList (x:xs) = getPriceFromItem x 
         + sumShoppingList xs 


getPriceFromItem :: ShoppingListItem -> PriceInCents 
getPriceFromItem (_, price) = price 

main :: IO() 
main = putStrLn ("Price of shopping list is " 
       ++ show (sumShoppingList shoppingList) 
       ++ " cents.") 

我试图运行它,并没有错误,但我不知道该怎么输入。我试过了,但我想我错了,因为我得到这个错误:

ERROR - Undefined data constructor

任何人都可以告诉我,我应该输入什么?

+0

所以也许我问的是错的?我用英文解释不好,但是当我运行这个程序时,我应该输入什么来使它完成它的工作? Idk如果我有道理 – Mel

+2

你能准确描述你是如何得到这个错误的吗?您的代码在编译时生成正确的输出(通过编译为可执行文件或将文件加载到'GHCi'中,然后调用'main')。 – hnefatl

回答

5

程序在写入时不会接受任何输入。用ghc编译将产生一个可用的可执行文件。或者,使用runhaskell运行也应该正常工作。

根据你的问题,我怀疑你是在ghci口译员里面运行的。在这种情况下,您可以使用:l filename.hs(或:r重新加载)加载文件,然后只需调用main即可运行主函数。

+0

oooh!谢谢你,这就是我的意思。非常感谢! – Mel

相关问题