相关疑难解决方法(0)

Haskell - 在'where'中定义一个带有警卫的功能

我刚刚开始自学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 where-clause parse-error

15
推荐指数
1
解决办法
1万
查看次数

是否有可能在Haskell中筑巢?

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)

haskell

7
推荐指数
4
解决办法
7810
查看次数

如何使用putStrLn的后卫?

我正在尝试编写一个函数来执行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)

haskell

1
推荐指数
1
解决办法
184
查看次数

标签 统计

haskell ×3

parse-error ×1

where-clause ×1