我想制作一个Haskell模块,用于计算车轮p
和车辆m
总数时车辆r
和摩托车n
的数量
我有以下功能:
p = (r - 2p) / n
m = n - ((r-2) / 2))
n = p + m
r = 4p + 2m
但是我如何将其定义为Haskell函数? 是开始
calculator :: Int -> Int -> Int -> Int
calculator r n = ...
如何正确撰写功能p
,m
,n
,r
来定义calculator
?
答案 0 :(得分:3)
你在这里遇到一些问题,你在几个地方遗漏了乘法运算符(必须是2 * p
而不是2p
),并且你的所有变量都没有功能但是相互定义的值,您的calculator
函数必须以小写字母开头,并且您应该使用div
而不是/
来表示整数(/
isn' t为Int
类型定义。您可能需要以下内容:
numCars :: Int -> Int -> Int
numCars wheels carriages = undefined {- formula for calculating number of cars -}
numBikes :: Int -> Int -> Int
numBikes wheels carriages = undefined {- formula for calculating number of motorcycles -}
calculator :: Int -> Int -> (Int, Int)
calculator wheels carriages = (numCars wheels carriages, numBikes wheels carriages)
main :: IO ()
main = do
putStr "Enter the number of wheels: "
wStr <- getLine
putStr "Enter the number of carriages: "
cStr <- getLine
let (cars, bikes) = calculator (read wStr :: Int) (read cStr :: Int)
putStr "The number of cars is: "
print cars
putStr "The number of bikes is: "
print bikes