Experts Haskell, show me how following code, written in C, will look like in Haskell?

```
defect_N=0;
for(i=N-k+1; i<=N; i++) {
defect_tmp=1;
for(n=1; n<=steps; n++)
for(j=0; j<=N; j++)
defect_tmp-=P_tau_LH[n*(N+1)*(N+1)+i*(N+1)+j];
if(defect_tmp>defect_N)
defect_N=defect_tmp;
printf("%d %lf\n",i,defect_tmp);
}
printf("defect of P_tau_LH=%lf=%e defect_0=%lf, defect_N=%lf\n", max_double (defect_0,defect_N), max_double (defect_0,defect_N), defect_0, defect_N);
```

asked October 3rd 19 at 03:41

2 answers

answered on October 3rd 19 at 03:43

Well, actually it would be better formulas you have provided not a program, but if I understand correctly the method of calculation it will be similar to:

In Haskell I wrote quite a bit, so maybe there's a better way.

```
module Main where
import Text.Printf
idef i steps nn =
1 - sum [ n*(n+1)**2 + i*(n+1) + j | j <- [0..nn], n <- [0..steps]]
calcAndPrint i steps nn = do
let res = idef i nn steps
printf "%d %f" i res
return res
defect k nn steps =
maximum [ calcAndPrint i steps nn | i <- [nn - k+1 .. nn]]
main = do
let defect_0 = ...
let defect_N = defect <k> <nn> <steps>
printf "defect of P_tau_LH=%f=%e\n" defect_0
</steps></nn></k>
```

In Haskell I wrote quite a bit, so maybe there's a better way.

I forgot to wrap the expression n*(n+1)**2 + i*(n+1) + j P_tau_LH, but I think the General principle is clear. - Creola commented on October 3rd 19 at 03:46

answered on October 3rd 19 at 03:45

If you take what is called the translation "head", then it will look not very nice, I guess (especially in my performance, as I must say that I'm not a connoisseur of Haskala — just interested =)

I tried to write something in first approximation (this is just a crude sketch that should be developed to a working version if necessary). It turned out something like this:

In General, I understand that functional languages often need to start from the input conditions for the formation of a correct algorithm for solving the problem, as the "literal realization of the" imperative code does not give the best results (to put it mildly =).

I tried to write something in first approximation (this is just a crude sketch that should be developed to a working version if necessary). It turned out something like this:

N = start = k = defect0 = calcDefectTemp defectStart i = let getInnerPairs steps N = [(i, j) | i <- [1..steps], j <- [0..N]] in let f acc (n, j) = acc (P_tau_LH !! (n*(N+1)*..... )) in foldl f defectStart (getInnerPairs steps N) calcDefectN defectTemps = maximum (0 : defectTemps) main = do let lst = map (\i -> (i, calcDefectTemp 1.0 i)) [N-k+1..N] mapM (\(idx, def) -> print $ show idx ++ "" ++ show def) lst let defectN = calcDefectN $ map (\(idx, def) -> def) lst print $ "defect of P_tau_LH=" ++ (show $ maximum [defect0, defectN]) ++ ... return ()

In General, I understand that functional languages often need to start from the input conditions for the formation of a correct algorithm for solving the problem, as the "literal realization of the" imperative code does not give the best results (to put it mildly =).

Never write) to Transfer the forehead, is the greatest evil which can only create a man who came from another paradigm (like me for example). In addition, do you was not too lazy to invent a sum and printf and manually drive parameters through function?) commented on October 3rd 19 at 03:48

Well so about what and speech ) Just an example of how this is _not_ happens when you attempt a "direct" translation ) commented on October 3rd 19 at 03:51

Find more questions by tags Haskell