Haskell allows this, you just have to actually demonstrate that the mutation is local:
statefulSum :: [Int] -> Int statefulSum xs = runST $ do n <- newSTRef 0 traverse (\x -> modifySTRef n (\y -> y + x)) xs readSTRef n
But Odersky's point is that (at least in Scala) it is even simpler (and thus should be preferred) to do it with raw (but still local!) mutation.
https://www.youtube.com/watch?v=QRcD9Zc7eq4&t=943s
I'm afraid we don't have anything like ST in any Scala library that I know of :(
Haskell allows this, you just have to actually demonstrate that the mutation is local: