使用递归而不是循环将C代码转换为haskell代码(无列表)

Dav*_*unn -1 recursion counter haskell loops nested

我想将以下c代码转换为haskell代码,而不使用列表。它返回两个数字出现的次数对于给定的n,其中n满足n=(a*a)*(b*b*b)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(void) {

int n = 46656;
int i,j,counter=0,res=1;


int tr = sqrt(n);


for(i=1; i<=tr; i++) {
    for(j=1; j<=tr; j++) {

                res = (i*i) * (j*j*j) ;
                if(res==n) {
            counter=counter+1;
        }
        printf("%d\n",res);
    }

}

printf("%d\n",counter);
}
Run Code Online (Sandbox Code Playgroud)

在循环方面,我设法在haskell中做了类似的事情,但仅是为了找到总和。我发现也很难在haskell中实现if部分和反部分(请参见c代码)。任何帮助,不胜感激!这也是我的haskell代码:

sumF :: (Int->Int)->Int->Int
sumF f 0 = 0
sumF f n = sumF f (n-1) + f n



sumF1n1n :: (Int->Int->Int)->Int->Int
sumF1n1n f 0 = 0
sumF1n1n f n = sumF1n1n f (n-1)
               +sumF (\i -> f i n) (n-1)
               +sumF (\j -> f n j) (n-1)
               +f n n

func :: Int->Int->Int
func 0 0 = 0
func a b = res
        where
        res = (a^2 * b^3)

call :: Int->Int
call n = sumF1n1n func n
Run Code Online (Sandbox Code Playgroud)

Dan*_*ner 5

我想惯用的翻译看起来像这样:

n = 46656
tr = sqrt n
counter = length
    [ ()
    | i <- [1..tr]
    , j <- [1..tr]
    , i*i*j*j*j == n
    ]
Run Code Online (Sandbox Code Playgroud)