stateful - Accumulator factory in Haskell -


now, @ start of adventure programming have problems understanding basic concepts. here 1 related haskell or perhaps functional paradigm.

here general statement of accumulator factory problem, http://rosettacode.org/wiki/accumulator_factory

[write function that]

  • takes number n , returns function (lets call g), takes number i, , returns n incremented accumulation of every call of function g(i).

  • works numeric type-- i.e. can take both ints , floats , returns functions can take both ints , floats. (it not enough convert input floats. accumulator has seen integers must return integers.) (i.e., if language doesn't allow numeric polymorphism, have use overloading or that)

  • generates functions return sum of every number ever passed them, not recent. (this requires piece of state hold accumulated value, in turn means pure functional languages can't used task.)

  • returns real function, meaning can use wherever use function had defined in ordinary way in text of program. (follow language's conventions here.)

  • doesn't store accumulated value or returned functions in way cause them inadvertently modified other code. (no global variables or other such things.)

with, understand, key point being: "[...] creating function [...] generates functions return sum of every number ever passed them, not recent. (this requires piece of state hold accumulated value, in turn means pure functional languages can't used task.)"

we can find haskell solution on same website , seems quote above says.

here http://rosettacode.org/wiki/category:haskell said haskell purely functional.

what explanation of apparent contradiction? or maybe there no contradiction , lack understanding? thanks.

the haskell solution not quite follow rules of challenge. in particular, violates rule function "returns real function, meaning can use wherever use function had defined in ordinary way in text of program." instead of returning real function, returns st computation produces function produces more st computations. within context of st "state thread", can create , use mutable references (stref), arrays, , vectors. however, it's impossible mutable state "leak" outside state thread contaminate pure code.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -