1
submitted 6 months ago by jaror@kbin.social to c/haskell@kbin.social

Declarative vs imperative:

let v = sum [ ((x ! i) - m) ^ 2 | i <- [0 .. cc - 1] ] / fromIntegral cc

float v = 0.0f;
for (int i = 0; i < C; i++) {
    float xshift = x[i] - m;
    v += xshift * xshift;
}
v = v/C;

#haskell

top 1 comments
sorted by: hot top controversial new old
[-] jaror@kbin.social 1 points 6 months ago

Admittedly it gets more complicated when summing two things at the same time:

let Pair dnormMean dnormNormMean =
      fold (Pair <$> dimap (\(Pair _ dnormI) -> dnormI) (/ fromIntegral cc) sum
                 <*> dimap (\(Pair normBti dnormI) -> normBti * dnormI) (/ fromIntegral cc) sum)
        $ map (\i -> Pair (((inp ! (off + i)) - meanBt) * rstdBt)
                          ((weight ! i) * (dout ! (off + i))))
          [0 .. cc - 1]

float dnorm_mean = 0.0f;
float dnorm_norm_mean = 0.0f;
for (int i = 0; i < C; i++) {
    float norm_bti = (inp_bt[i] - mean_bt) * rstd_bt;
    float dnorm_i = weight[i] * dout_bt[i];
    dnorm_mean += dnorm_i;
    dnorm_norm_mean += dnorm_i * norm_bti;
}
dnorm_mean = dnorm_mean / C;
dnorm_norm_mean = dnorm_norm_mean / C;

this post was submitted on 11 Apr 2024
1 points (100.0% liked)

Haskell

4 readers
1 users here now

**The Haskell programming language community.** Daily news and info about all things Haskell related: practical stuff, theory, types, libraries, jobs, patches, releases, events and conferences and more... ### Links - Get Started with Haskell

founded 1 year ago