我刚刚开始自学Haskell.这段代码应该进行素数分解:
divides :: Integer -> Integer -> Bool
divides small big = (big `mod` small == 0)
lowestDivisor :: Integer -> Integer
lowestDivisor n = lowestDivisorHelper 2 n
where lowestDivisorHelper m n
| (m `divides` n) = m -- these should belong to lowestDivisorHelper
| otherwise = lowestDivisorHelper (m+1) n
primeFactors :: Integer -> [Integer]
primeFactors 1 = []
primeFactors n
| n < 1 = error "Must be positive"
| otherwise = let m = lowestDivisor n
in m:primeFactors …Run Code Online (Sandbox Code Playgroud) Haskell新手在这里,试图编写代码来解析数学表达式.码:
isDigit :: Char -> Bool
isDigit c = c >= '0' && c <= '9'
parseNumber :: String -> Maybe (String, String)
parseNumber [] = Just ("", "")
parseNumber (h:ls)
| isDigit h
| p == Nothing = Just([h], ls) -- Digit found <<< ERROR!!
| otherwise = Just (h:fst d, snd d) -- Ends in a digit
| h == '.'
| p == Nothing = Nothing -- Ends in a point
| not ('.' `elem` (snd d)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数来执行2种不同的游戏模式,这些游戏模式被定义为tictactoe :: IO ()和main :: IO ().我输入'|'时出现解析错误.我不明白我做错了什么.有人可以向我解释一下吗?
tictac :: IO()
tictac = do
putStrLn "Would you like to play against the computer or
another player? Enter: 2 Player or Computer"
choice <- getLine
|choice == "Computer" = main
|choice == "2 Player" = tictactoe
|otherwise = putStrLn "That's not a choice!"
Run Code Online (Sandbox Code Playgroud)