https://wiki.haskell.org/api.php?action=feedcontributions&user=Toraritte&feedformat=atomHaskellWiki - User contributions [en]2020-02-29T07:23:50ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Category:Theoretical_foundations&diff=62507Category:Theoretical foundations2018-06-05T11:16:08Z<p>Toraritte: </p>
<hr />
<div>Things (related to Haskell) that give us the feeling of surprise instead of design, the way natural sciences and mathematics do.<br />
<br />
[http://www.smithsonianeducation.org/educators/lesson_plans/eskimo/page05.html In an Eskimo tale], the Raven, after having created the world, does not recognize his own creatures. He creates a marrow, and after a while, a man steps out of the split marrow. -- Who are you and how do you get here? -- asks Raven his own creature. In another tale, a creature is creating himself, astonishing the god.<br />
<br />
In <math>\lambda</math>-calculus and combinatory logic, the existence of [https://en.wikipedia.org/wiki/Church_encoding Church numerals] was a surprise, too. They were not anticipated, so not a mere design.<br />
Other things can be astonishing too -- like the mere existence of [http://www.madore.org/~david/computers/quine.html self-replicating programs (quines)], a consequence of the fixed point theorem.<br />
<br />
Haskell is based on and related to powerful ideas, and learning Haskell can yield a feeling of exploring something out there, instead of watching a huge cathedral.<br />
<br />
[[Category:Haskell]]</div>Torarittehttps://wiki.haskell.org/index.php?title=Category:Theoretical_foundations&diff=62506Category:Theoretical foundations2018-06-05T11:13:31Z<p>Toraritte: </p>
<hr />
<div>Things (related to Haskell) that give us the feeling of surprise instead of design, the way natural sciences and mathematics do.<br />
<br />
[http://www.smithsonianeducation.org/educators/lesson_plans/eskimo/page05.html In an Eskimo tale], the Raven, after having created the world, does not recognize his own creatures. He creates a marrow, and after a while, a man steps out of the split marrow. -- Who are you and how do you get here? -- asks Raven his own creature. In another tale, a creature is creating himself, astonishing the god.<br />
<br />
In <math>\lambda</math>-calculus and combinatory logic, the existence of [https://en.wikipedia.org/wiki/Church_encoding Church numerals] was a surprise, too. They were not anticipated, so not a mere design.<br />
Other things can be astonishing too -- like the mere existence of self-replicating programs (quines), a consequence of the fixed point theorem.<br />
<br />
Haskell is based on and related to powerful ideas, and learning Haskell can yield a feeling of exploring something out there, instead of watching a huge cathedral.<br />
<br />
[[Category:Haskell]]</div>Torarittehttps://wiki.haskell.org/index.php?title=Worker_wrapper&diff=62504Worker wrapper2018-06-04T12:44:14Z<p>Toraritte: </p>
<hr />
<div>It is sometimes easier or more efficient to write functions which have particular "start arguments" or that pass state. When this is the case, write wrappers rather than trying to code within the original signature.<br />
==Accumulator examples==<br />
e.g. the function reverse<br />
<haskell><br />
reverse :: [a] -> [a]<br />
</haskell><br />
could be written<br />
<haskell><br />
reverse [] = []<br />
reverse (x:xs) = reverse xs ++ [x]<br />
</haskell><br />
however this will be more efficient if it were written<br />
<haskell><br />
reverse xs = revWorker [] xs<br />
<br />
revWorker s [] = s<br />
revWorker s (x:xs) = revWorker (x:s) xs<br />
</haskell><br />
note often a worker will also return some state along with a result which can be stripped away.<br />
<br />
<hask>revWorker</hask> could be considered a generally useful function. <hask>revWorker x y</hask> is equivalent to, but probably faster than, <hask>reverse y ++ x</hask>.<br />
<br />
<br />
==Other examples==<br />
Wrappers are often used to play the role of loop initialisation in imperative languages. For example:<br />
<haskell><br />
fib n = fibWorker n 0 1<br />
<br />
fibWorker n f1 f2<br />
| n == 0 = f1<br />
| otherwise = fibWorker (n-1) f2 (f1+f2)<br />
</haskell><br />
<br />
==Hiding the worker==<br />
Also, often one hides the worker(s) with a where (or let). E.g. :<br />
<haskell><br />
fib = fibWorker 0 1<br />
where<br />
fibWorker f0 f1 n<br />
| n == 0 = f0<br />
| True = fibWorker f1 (f0 + f1) (n-1)<br />
</haskell><br />
Of course, one then has to de-hide the worker if one want to test it with different initial arguments.<br />
<br />
In some cases, though, the worker can be a generally useful function on its own merits, in that case one obviously shouldn't hide it.<br />
<br />
<br />
One common case is where you want to subject the worker function to [[Unit testing]]. In such a situation, the test suite has to be able to get to the worker function. Another is where the worker might be a candidate for further abstraction. (See [[Higher order function]] for examples.)<br />
<br />
Moving a worker to a higher level (which may require some [[Lambda lifting]]) is known as [[Let floating]].<br />
<br />
<br />
==See also==<br />
* [[Avoiding parameter passing]]<br />
* [http://www.cs.nott.ac.uk/~gmh/wrapper.pdf The Worker Wrapper Transformation]<br />
<br />
[[Category:Idioms]]</div>Toraritte