星期六, 9月 25, 2010

Haskell Practice - Parenthesis Balance

這題其實用什麼語言寫都差不多其實還是會有差,只是我才疏學淺,T55555 在他的 blog article 中寫出幾個相當好的範例!! 只是剛好出了 Unix 助教作業,就連帶用 Haskell 練習一下


check_string :: String -> Bool
check_string s = check_stack s [] 

check_stack :: String -> String -> Bool
check_stack [] s = length s == 0
check_stack (x:xs) s
    | x == '[' || x == '(' = check_stack xs (x:s)
    | otherwise = case s of
                      [] -> False
                      s:ss -> if (x == ']' && s == '[') || (x == ')' && s == '(') then check_stack xs ss 
                              else False


---
下次該來練習 Tree 了。

張貼留言