2011-04-17 166 views
0

我想製作一個簡單的程序,在其中我可以添加矩形列表(id,width,height)並顯示帶specyfic id(id,width,height)的矩形 - 我想我有在下面的代碼中有多個錯誤 - 你知道什麼可能是錯的嗎?我將命令「menuRectangles」(現在這不工作)運行以下命令:將矩形添加到列表中並顯示矩形

import IO 
import Char 

menuRectangles = do 
    putStrLn "Please choose option:" 
    putStrLn "1 - Add rectangle" 
    putStrLn "2 - Show rectangle" 
    putStrLn "3 - Quit" 
    putStr "Number: "; 
    n <- getLine 
    case n of 
     "1"   -> do addRectangle; menuRectangles; 
     "2"   -> do showRectangle; menuRectangles; 
     "3"   -> menuRectangles; 
     otherwise -> putStrLn "The End"; 

addRectangle = do 
    putStrLn "Id: " 
    id <- getLine 
    putStrLn "Width: " 
    width <- getLine 
    putStrLn "Height: " 
    height <- getLine 
    addingRectangle (Rectangle id width height); 

showRectangle = do 
    putStrLn "Choose id rectangle: " 
    id <- getLine 
    showingRectangle r[id]; 


data RectangleType = Rectangle Int Int Int deriving(Show) 

addingRectangle r [] = [r] 
addingRectangle r rx = r:rx 

showingRectangle (Rectangle id width height) = "id: " ++ show id ++ "width: " ++ show width ++ "height: " ++ height ++ "\n"; 
+4

您將需要詢問更具體的問題。究竟出了什麼問題,你想做什麼。簡單地列出你的代碼是不可接受的。 – 2011-04-17 17:55:20

+0

我只想指出,您必須使用非常舊的教科書或學習資料,因爲像「import char」這樣的舊式不合格進口不再使用。 – 2011-04-17 18:18:07

+0

我有錯誤:不在範圍內:'r' – mrquestion 2011-04-17 18:49:22

回答

1

的解決方案是在menuRectangles使用一個累積列表。

當你調用addRectangles你要的結果,以一個新的變量綁定 - 這就是這個表情是這樣做的:

rs_new <- addRectangle rs 

注意 - 這是在Haskell特殊的語法的IO或一元表達式中結合。

在Haskell其他結合形式是let

let xs = [2,3,4] 
    ys = 1 : xs 
in length ys 

這是你的程序的工作版本 - 請注意,你現在必須與main運行,因此它可以播種menuRectangles與空列表。當爲菜單選項遞歸調用menuRectangles時,您必須傳遞累加列表。

import IO 
import Char 

main :: IO() 
main = do 
    xs <- menuRectangles [] 
    mapM print xs 
    return() 

-- A new helper - this will throw an error 
-- if you don't type a number. There are 
-- plenty of ways to make it more robust. 
-- 
getInt :: IO Int 
getInt = do 
    xs <- getLine 
    return (read xs) 



menuRectangles :: [RectangleType] -> IO [RectangleType] 
menuRectangles rs = do 
    putStrLn "Please choose option:" 
    putStrLn "1 - Add rectangle" 
    putStrLn "2 - Show rectangle" 
    putStrLn "3 - Quit" 
    putStr "Number: " 
    n <- getLine 
    case n of 
     "1"   -> do { rs_new <- addRectangle rs; menuRectangles rs_new }; 
     "2"   -> do { showRectangle rs; menuRectangles rs } 
     "3"   -> do { putStrLn "Quitting"; return rs } 
     otherwise -> do { putStrLn "The End"; return rs } 

addRectangle :: [RectangleType] -> IO [RectangleType] 
addRectangle rs = do 
    putStrLn "Id: " 
    id <- getInt 
    putStrLn "Width: " 
    width <- getInt 
    putStrLn "Height: " 
    height <- getInt 
    let new_rs = addingRectangle (Rectangle id width height) rs 
    return new_rs 


showRectangle :: [RectangleType] -> IO() 
showRectangle rs = do 
    putStrLn "Choose id rectangle: " 
    id <- getInt 
    -- Need a lookup 
    case findRectangle id rs of 
     Nothing -> putStrLn ("Not found " ++ show id) 
     Just rect1 -> putStrLn (showingRectangle rect1) 



data RectangleType = Rectangle Int Int Int deriving(Show) 

addingRectangle :: RectangleType -> [RectangleType] -> [RectangleType] 
addingRectangle r [] = [r] 
addingRectangle r rx = r:rx 

showingRectangle :: RectangleType -> String 
showingRectangle (Rectangle id width height) = 
    "id: " ++ show id ++ "width: " ++ show width ++ 
    "height: " ++ show height ++ "\n"; 

-- We have to use Maybe - Just rectangle OR Nothing 
-- to account for the case where the id does not match 
-- 
findRectangle :: Int -> [RectangleType] -> Maybe RectangleType 
findRectangle _ [] = Nothing 
findRectangle n ((Rectangle i w h):xs) = 
    if n == i then Just (Rectangle i w h) else findRectangle n xs 
+0

Thx你非常:))! – mrquestion 2011-04-17 20:06:51