如何在Haskell中一起写整数和字符串?

问题描述 投票:-5回答:1
coin :: Int -> Int -> Int -> Int -> Int
dollar :: Int -> Int -> Int -> Int -> Int
sen :: Int -> Int -> Int -> Int -> Int

coin q d n p = (q * 25) + (d * 10) + (n * 5) + (p * 1)
dollar q d n p = (coin q d n p) `div` 100
sen q d n p = (coin q d n p) `mod` 100

假设(dollar q d n p)为6,(sen q d n p)为27。

如何在Haskell中为输出写入"6 dollar and 27 sen"

我可以用Python完成,

# [Input]

    dollar = 2 * 3
    sen = 3 * 9
    print(dollar, " dollar and ", sen, "sen")

# [Output]
# 6 dollar and 27 sen

然后,如何在Haskell中编写它,以便输出可以相同?

haskell output
1个回答
1
投票
putStrLn $ (show dollar) ++ " dollar and " ++ (show sen) ++ "sen"

或(使用模块Text.Printf

Text.Printf
© www.soinside.com 2019 - 2024. All rights reserved.