[Haskell-beginners] explaining effects

Alexander Berntsen alexander at plaimi.net
Tue Dec 29 11:00:50 UTC 2015


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Rein explains effects well enough. Now for side-effects: a side-effect
is what is allowed to happen in the absence of a weak equivalence
between call-by-name (including memoised versions) and call-by-value[0].

Let's play "spot the side-effect!"

- -- Haskell function for adding two numbers.
f x y = x + y

- -- function in a language that happens to be syntactically like Haskell.
f x y = print "Hallo!" >> x + y

- -- program to evaluate the function.
- -- it just happens to be equal in Haskell and the Haskell-like language.
main = let a = f 4 2 in print a >> print a

evaluation-strategy     | call-by-need | call-by-value
- -------------------------------------------------------------
x + y                   | 6\n6         | 6\n6
print "Hallo" >> x + y  | Hallo!\n6\n6 | Hallo!\n6\nHallo!\n6

See how the latter function differs in call-by-need and call-by-value?
This is because it has a *side-effect* of printing stuff. This is not
allowed in Haskell. And in fact, the latter function is impossible in
Haskell.

As Rein was getting at by abstraction leaks -- side-effects have an
observable interaction with the outside world.

[0]  <https://www.cs.indiana.edu/~sabry/papers/purelyFunctional.ps>
- -- 
Alexander
alexander at plaimi.net
https://secure.plaimi.net/~alexander
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCgAGBQJWgmfhAAoJENQqWdRUGk8BZfkQAOft2e7RmRheu0ZGAXXj3P4m
4zPnJ5SQ//+TTM2rzH1b6ifoidGgEhKiyDr2IajXbj0wIbPLXqORByXUFx6E6I1q
rz9rslEb2XGwxTOm/Wi90IGO6niEPO1+/WCFCLE2G+Fh2M3uo381djlQfaS1MVla
1UtzElnX2bnpLfjeSjMFFB2joEuCCl8Gz+QXVGIX1H3BE+xSyM+vllDtkwob/6Bn
OyPckgGyiQlt5RpPoBsdEBU5qzrT5yJWaRWeiRIg293XrOPls3kM0GvhvaAFXmWr
1A/L391BQtw65xeGTlPArCi+xemwVsgwK2hdQWPmTrpJATveV0vN1OhQMkiI5b+Q
5SfKo0MKKWezRu7avIfaJ0IUB5Pl/FM+IhMFoHWM4oE5ixh7Vr91nslhOMjvAWgR
GzKyMuY63CLoy/1He5nNoCJ2Pjwdf65lUOD/sNJhjLbd2qw220UFE4L2SOLXLX5U
uBY1GF3C+biH2ai7utKU9RBXyV7p5dVcV4vft+Eb118QJmp3fFP26HS6IwuD3V8q
JNWAo7ZTK5vpujUHtee2J1ltHrleSlVaaJE0ONDzKDzi7QrwUAUbae3/Wnzy5zBF
ZXSPqABh8MNjkItax5zQwHyvgcJTRNtt7xj41+d7WKscgy5XvDQC3/RSQPXYZ9Ib
upR6CDDSP2bns7KfRDU0
=AGvd
-----END PGP SIGNATURE-----


More information about the Beginners mailing list