Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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
Loops Haskell中递归次数的计算_Loops_Haskell_Recursion_Counting - Fatal编程技术网

Loops Haskell中递归次数的计算

Loops Haskell中递归次数的计算,loops,haskell,recursion,counting,Loops,Haskell,Recursion,Counting,我正在Haskell写一份小作业,以确定两个给定日期之间的距离。我编写了一个粗略的函数来循环日期,但我不知道如何用函数编程的方式编写循环。我以前只做过程序和面向对象编程。我需要存储我调用nextDate函数多少次的信息,但是Haskell不允许我在函数中引入变量。这是我到目前为止找到的代码。这一点也不是很简单 nextDate year month day = if day + 1 < 31 then (year,month, day+1) else if

我正在Haskell写一份小作业,以确定两个给定日期之间的距离。我编写了一个粗略的函数来循环日期,但我不知道如何用函数编程的方式编写循环。我以前只做过程序和面向对象编程。我需要存储我调用nextDate函数多少次的信息,但是Haskell不允许我在函数中引入变量。这是我到目前为止找到的代码。这一点也不是很简单

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)
nextDate年-月-日=
如果第+1天<31天
然后(年、月、日+1)
如果月份+1小于12,则为其他
然后(年、月+1、1)
其他(年份+1,1,1)
计算差异年月日年2月2日2=
设x=0
如果年=year2&&month==month2&&day==day2,则x
其他的
下一个日期(年、月、日)
x=x+1
--我将如何在Python中实现它
--x=0
--而((tuple1)!=(第二年、第二个月、第二天)):
--x+=1
--tuple1=nextDate(tuple1)
--打印(x)

如果要跟踪函数被调用的次数,需要将其作为输入提供。没有其他方法可以做到这一点,因为Haskell只允许处理传递到函数中的参数

例如,假设我想计算一个阶乘,但我想跟踪它需要多少步。我的函数签名可能如下所示:

factorial::Int->(Int,Int)--获取一个数字,返回数字和递归计数
factorialInternal::(Int,Int)->(Int,Int)--这实际上执行递归
然后定义可以如下所示:

factorial n=factorialInternal(n,0)
阶乘内部(1,n)=(1,n+1)
阶乘内部(x,n)=let(y,z)=阶乘内部(x-1,n)in(x*y,z+1)
从本质上讲,跟踪递归量的参数在每一级递增,然后成为
factorial
输出的一部分

创建一个接口函数绝对有助于您在使用该函数时不必手动输入起始递归级别(无论如何,该级别始终为零)。函数签名可能是什么样子的示例:

——您调用的函数
计算差异::(Int,Int,Int)->(Int,Int,Int)->Int
--calculateDifference函数调用的内容(第三个参数是递归计数器)
计算差异内部::(Int,Int,Int)->(Int,Int,Int)->Int->Int
从这里,您应该能够了解如何实现
calculateDifference
calculateDifferenceInternal


编辑:正如amalloy指出的,一个更好的解决方案是只输出计数器,而不是输入一个:因此,与其使用
factorialInternal::(Int,Int)->(Int,Int)
factorialInternal Int->(Int,Int)
工作。然后定义如下:

factorialinternal1=(1,0)
阶乘内部n=let(x,y)=阶乘内部(n-1)in(n*x,y+1)

可能重复“没有其他方法可以做到这一点”——不完全是:您可以使用新的输出,而不使用新的输入。在基本情况下,为计数器返回0,在递归情况下,为递归调用生成的结果添加1。没错,这是解决此问题的另一种方法。我会修改我的答案并加进去。我不会说我的建议更好。这是不同的,它将更好地执行某些功能,但对其他功能更差。知道这两件事真是太好了。