如何在拥抱中转移两个功能?

时间:2013-11-05 14:34:25

标签: haskell

我想制作一个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 = ...

如何正确撰写功能pmnr来定义calculator

1 个答案:

答案 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