2017-05-08 22 views
1

我在基於圖形的haskell中創建社交網絡。問題是我想爲人們創建一個用戶界面進行交互。此用戶界面應包含顯示選項的文本,並基於鍵輸入,用戶可以搜索數據庫並對數據集執行操作。Haskell中的用戶界面和菜單

我已經這樣做:

-Explored這個線程「Simple text menu in Haskell

-Installed包如haskelline

問題是,我剛開始學習Haskell和進度一直該死與其他語言的經驗相比,速度較慢,所以我沒有從上述資源中獲益,這在一定程度上解決了我的問題。

有人會友善地分享由CL界面菜單屏幕組成的模板。這要求用戶按下爲不同功能映射的按鍵。然後輸出後,返回到主菜單。

這裏是我的功能看起來像一個例子。

isFriend :: Node -> Node -> [Edge] -> Bool 
isFriend _ _ [] = False 
isFriend a b (x:xs) 
| edgeCompare a b x == True = True 
| otherwise = isFriend a b xs 

所以菜單可以說「按下我」來搜索朋友,這會提示用戶輸入一個名字。這將使用用戶輸入的參數運行我的函數。

謝謝:)

+0

確切的東西在您引用的線程中提供。因此你的問題非常含糊......你想要更深入的解釋嗎? – Jedai

+0

進展緩慢或多或少是預期的。當一個人習慣於一種編程語言範例,並轉向另一種語言時,需要付出巨大的努力。我猜想從命令式和程序式轉向命令式的OOP比命令式的OOP更容易,尤其是像Haskell這樣的純功能。然而,正因爲它如此不同,所以值得學習。 – chi

回答

0

基於this answer,我已經創造了這個:

import System.IO (stdin, hSetEcho, hSetBuffering, BufferMode(NoBuffering), hReady) 
import Control.Monad (when) 

getKey = reverse <$> getKey' "" 
    where getKey' chars = do 
      char <- getChar 
      more <- hReady stdin 
      (if more then getKey' else return) (char:chars) 

-- Simple menu controller 
main = do 
    hSetBuffering stdin NoBuffering 
    hSetEcho stdin False 
    key <- getKey 
    when (key /= "\ESC") $ do 
    case key of 
     "w"  -> putStrLn "↑" 
     "s"  -> putStrLn "↓" 
     "d"  -> putStrLn "→" 
     "a"  -> putStrLn "←" 
     "\n" -> putStrLn "⎆" 
     "\DEL" -> putStrLn "⎋" 
     _  -> return() 
    main 

[1還示出如何檢測箭頭按鍵,這就是爽!改變stdin的行緩衝很重要。

您可能希望您的應用程序循環有一些初始應用程序狀態,並將其作爲參數傳遞。

+1

謝謝。這幫了很多 –