Haskell中是否有“继续”?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haskell中是否有“继续”?相关的知识,希望对你有一定的参考价值。
我最近开始学习Haskell,目前正在尝试编写基本的Haskell函数。
我已经编写了一个名为intToRoman
的函数,该函数应该将整数转换为罗马数字。它将整数中的数字除以整数(1400-> [1,4,0,0]),然后将每个数字转换为罗马数字,并考虑列表的长度,以确定何时是一千或一百。
但是,它不会停止并且也会检查零。例如,数字1400将返回:
MCD** Exception: Map.!: given key is not an element in the map
CallStack (from HasCallStack)
这里是代码本身:
mapInttoRoman:: M.Map Int String
mapInttoRoman = M.fromList
[(1,"I"),(4,"IV"),(5,"V"),(9,"IX"),(10,"X"),(40,"XL")
,(50,"L"),(100,"C"),(400,"CD"),(900,"CM"),(500,"D"),(1000,"M")]
listOfInt :: Int -> [Int]
listOfInt 0 = [ ]
listOfInt c = listOfInt(c`div`10) ++ [c`mod`10]
duplicate :: String -> Int -> String
duplicate string n = concat $ replicate n string
intToRoman :: Int -> String
intToRoman 0 = ""
intToRoman c = createInt x (len-1)
where x = listOfInt c
len = length x
createInt y xz = findRoman (head y) xz ++ createInt(drop 1 y)(xz-1)
where findRoman b l
| l < 1 = mapInttoRoman M.! b
| b == 0 = " "
| l == 3 = duplicate (mapInttoRoman M.! (10^l)) b
| b == 1 = mapInttoRoman M.! (10^l)
| otherwise = mapInttoRoman M.! (b*10^l)
答案
我认为您使用的算法不正确。并且您使它变得比需要的更加专注。
也未完成列表,我添加了90和900个案例。
查看此代码..我认为它非常简单,您将轻松获得它。
将合格的Data.Map导入为M
mapInttoRoman:: M.Map Int String
mapInttoRoman = M.fromList
[(1,"I"),(4,"IV"),(5,"V"),(9,"IX"),(10,"X"),(40,"XL")
,(50,"L"),(90,"XC"),(100,"C"),(400,"CD"),(500,"D"),(900,"CM"),(1000,"M")]
intToRoman :: Int -> String
intToRoman 0 = ""
intToRoman n | M.member n mapInttoRoman = mapInttoRoman M.! n
| otherwise = val ++ intToRoman (n-key)
where
keys = reverse (M.keys mapInttoRoman)
key = maxValidKey n keys
val = mapInttoRoman M.! key
maxValidKey n (k:ks)
| n >= k = k
| otherwise = maxValidKey n ks
测试:
以上是关于Haskell中是否有“继续”?的主要内容,如果未能解决你的问题,请参考以下文章