4

When running hlint over the weightDelta function is keeps suggesting Eta reduce. I read another related Eta reduce question, but I can't seem to transfer the understanding into this case.

module StackQuestion where

import qualified Data.Vector as V

type Weights = V.Vector Double
type LearningRate = Double

weightDelta :: LearningRate -> Double -> Double -> Weights -> Weights
weightDelta n r y ws = V.map update  ws
        where update w = diff * n * w
              diff = r - y

Every change I try to make to "reduce" it to point free syntax just breaks it. Where's the change to be made, and is there any sort of intuition or trick to avoid an eta reduce suggestion in the future?

Community
  • 1
  • 1
enjoylife
  • 3,801
  • 1
  • 24
  • 33
  • Note that one eta-reduce transformation applies to one single argument, specifically the final explicit argument which is 'ws' in your case. – Chris Kuklewicz Jul 08 '12 at 09:06
  • 1
    Just as a note, these reductions (and more) can be done automatically by the `pointfree` program (`cabal install pointfree`), so you can experiment to test/extend your understanding. (e.g. it says that the fully pointfree version is `weightDelta = (((V.map . (*)) .) .) . (. (-)) . (.) . (*)`.) – huon Jul 09 '12 at 07:52

1 Answers1

8

You won't get it to point- free syntax easily, but what you can do immediately is just η-reduce the ws away.

weightDelta :: LearningRate -> Double -> Double -> Weights -> Weights
weightDelta n r y = V.map update
        where update w = diff * n * w
              diff = r - y

You can also do something like

        where update = (δ *)
              δ = n * (r - y)

but that's rather debatable.

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319