【发布时间】:2019-06-10 19:03:00
【问题描述】:
我正在用 Haskell 写一个小学校作业,以确定两个给定日期之间的距离。我编写了一个粗略的函数来循环日期,但我无法理解如何以函数式编程方式编写循环。我以前只做过程序和 OOP 编程。我不知何故需要存储我调用 nextDate 函数的次数的信息,但 Haskell 不允许我在函数中引入变量。这是我到目前为止提出的代码。这根本不是 Haskelly...
nextDate year month day =
if day + 1 < 31
then (year,month, day+1)
else if month + 1 < 12
then (year, month + 1, 1)
else (year +1,1,1)
calculateDifference year month day year2 month2 day2 =
let x = 0
if year == year2 && month == month2 && day == day2 then x
else
nextDate(year, month, day)
x = x + 1
-- How I would do it in Python
-- x = 0
-- while((tuple1) != (year2, month2, day2)):
-- x += 1
-- tuple1 = nextDate(tuple1)
-- print(x)
【问题讨论】:
标签: loops haskell recursion counting