我从用户那里获得输入(x),通过让y =(读取x):: Int将其转换为Int,然后我希望函数在用户什么都不给的情况下以特殊方式运行(空字符串).
-- In this place I would like to handle situation in which user
-- gave empty string as argument
-- this doesnt work :/
yearFilter [] y = True
--This works fine as far as y is integer
yearFilter x y | x == (objectYear y) = True
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助,再见
Nor*_*sey 14
也许你想要一个Maybe类型?如果用户输入空字符串,则函数返回Nothing; 否则它返回Just n,n用户输入的是什么?
userInt :: String -> Maybe Int
userInt [] = Nothing
userInt s = Just $ read s
Run Code Online (Sandbox Code Playgroud)
(我还没有编译这段代码.)
除非明确定义,否则不存在 NULL。您可以像这样检查空字符串。
readInput :: IO ()
readInput = do
ln <- getLine
if valid ln
then -- whatever
else -- whatever
valid x
| null x = False
| not istJust convert x = False
| otherwise = True
where convert :: String -> Maybe Int
convert = fmap fst $ listToMaybe . reads $ "f"
Run Code Online (Sandbox Code Playgroud)