Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 返回日历时间值_Date_Haskell_Time - Fatal编程技术网

Date 返回日历时间值

Date 返回日历时间值,date,haskell,time,Date,Haskell,Time,用户给出:年、月、日、小时和分钟,我想使用这些值返回CalendarTime值。我该怎么做?因为我有一个错误:“无法将预期的IO CalendarTime类型与推断的Int类型匹配”。getDateTime函数有什么问题 命题:所有值都是正确的,例如月份范围是1-12等-我删除了下面代码中的验证,因为否则代码太长 isInteger i = not (null i) && all isDigit i getInteger :: String -> IO Int getIn

用户给出:年、月、日、小时和分钟,我想使用这些值返回CalendarTime值。我该怎么做?因为我有一个错误:“无法将预期的IO CalendarTime类型与推断的Int类型匹配”。getDateTime函数有什么问题

命题:所有值都是正确的,例如月份范围是1-12等-我删除了下面代码中的验证,因为否则代码太长

isInteger i = not (null i) && all isDigit i

getInteger :: String -> IO Int
getInteger q = do
    putStr q;
    i <- getLine
    if isInteger i == False then do
            putStrLn "Bad number"
            getInt q
        else return (read i)

getDateTime :: String -> IO CalendarTime
getDateTime question = do
    putStr question;
    year <- getInteger "Year: "
    month <- getInteger "Month: "
    day <- getInteger "Day: "
    hour <- getInteger "Hour: "
    minute <- getInteger "Minute: "
    return CalendarTime(year month day hour minute 0)
isInteger i=not(null i)&全部isDigit i
getInteger::String->IO Int
getInteger q=do
putstrq;
我知道时间
getDateTime问题=do
putStr问题;

year看起来您正试图将构造函数
CalendarTime
用作其他语言样式的函数

return (CalendarTime year month day hour minute 0)

看起来您正试图将构造函数
CalendarTime
用作其他语言样式的函数

return (CalendarTime year month day hour minute 0)

您没有说哪一行给出了错误,但我猜问题是:

return CalendarTime(year month day hour minute 0)
在Haskell中,括号仅用于分组。函数应用是通过一个接一个地编写一个术语来实现的
return
应应用于
CalendarTime
值,因此您可能希望改为:

return (CalendarTime year month day hour minute 0)
虽然这会更为惯用:

return $ CalendarTime year month day hour minute 0

您没有说哪一行给出了错误,但我猜问题是:

return CalendarTime(year month day hour minute 0)
在Haskell中,括号仅用于分组。函数应用是通过一个接一个地编写一个术语来实现的
return
应应用于
CalendarTime
值,因此您可能希望改为:

return (CalendarTime year month day hour minute 0)
虽然这会更为惯用:

return $ CalendarTime year month day hour minute 0
这条线

return CalendarTime(year month day hour minute 0)
被编译器读取为

return CalendarTime (year month day hour minute 0)
所以这不是对
日历时间的调用。相反,当参数返回时,您正在输入
CalendarTime
(年-月-日-小时-分钟0)
。这是胡说八道,因为
return
只接受一个参数,
year
不是函数。你可能是说

return (CalendarTime year month day hour minute 0)
尽管这仍然不起作用,因为
CalendarTime
需要更多的参数。(假设我们讨论的是
System.Time
中的一个,因为您没有指定导入)

你可能想要

return $ CalendarTime { ctYear = year
                      , ctMonth = toEnum (month-1)
                      , ctDay = day
                      , ctHour = hour
                      , ctMinute = minute
                      , ctSec = 0 })
这使得缺少的字段未定义,有关详细信息,请参阅

您还在第一个函数的递归调用中将
getInteger
拼写为
getInt
,但我假设这是您清理验证代码的结果。

这一行

return CalendarTime(year month day hour minute 0)
被编译器读取为

return CalendarTime (year month day hour minute 0)
所以这不是对
日历时间的调用。相反,当参数返回时,您正在输入
CalendarTime
(年-月-日-小时-分钟0)
。这是胡说八道,因为
return
只接受一个参数,
year
不是函数。你可能是说

return (CalendarTime year month day hour minute 0)
尽管这仍然不起作用,因为
CalendarTime
需要更多的参数。(假设我们讨论的是
System.Time
中的一个,因为您没有指定导入)

你可能想要

return $ CalendarTime { ctYear = year
                      , ctMonth = toEnum (month-1)
                      , ctDay = day
                      , ctHour = hour
                      , ctMinute = minute
                      , ctSec = 0 })
这使得缺少的字段未定义,有关详细信息,请参阅


在第一个函数的递归调用中,您还将
getInteger
拼写为
getInt
,但我假设这是您清理验证代码的结果。

thx非常严重-您的解决方案给了我警告,所以我添加了,现在可以了-thx:,ctPicosec=0,ctWDay=Monday,ctYDay=0,ctTZName=“”,ctTZ=0,ctIsDST=Falsethx非常-您的解决方案向我发出警告,因此我已添加,现在一切正常-thx:,ctPicosec=0,ctWDay=Monday,ctYDay=0,ctTZName=“”,ctTZ=0,ctIsDST=False样式点:不要说“如果foo==False,那么……”;说“如果不是福,那么……”。在本例中,它将是“if not$isInteger i then…”样式点:不要说“if foo==False then…”;说“如果不是福,那么……”。在这种情况下,它将是“如果不是$isInteger i,那么…”