Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Referential transparency

Name: Anonymous 2009-07-28 15:02

A function is RT <=> it can be substituted by its value in any place of the code, right? So what's the difference between a function being referentially transparent and not having side effects? Can I get an example of a function that is one but isn't the other one?

Name: Anonymous 2009-07-28 15:07

time()

Name: Anonymous 2009-07-28 15:09

>>2
That's not OCaml, but OK. Can mods close this thread now?

Name: Anonymous 2009-07-28 16:05

scheme:

(lambda (x) (+ x x))
(newline)
(current-input-port)
(random)

Should run the gamut.

Name: Anonymous 2009-07-28 16:35

time() has a side effect if you consider I/O to always be a side effect.

Name: Anonymous 2009-07-28 16:40

run the gamut.
What are you, Jewish?
I told you, close this thread, my question has been answered.

Name: Anonymous 2009-07-28 16:41

>>4
transparent, without side effects
not transparent, with side effects
not transparent, without side effects
not transparent, with side effects

>>5
Is reading a global variable a side effect?

Name: Anonymous 2009-07-28 16:47

>>7
Is reading a global variable a side effect?
The function is accessing data other than its arguments, so yes.

Name: Anonymous 2009-07-28 16:51

>>8

in order to run the function you have to have a computer-equivalent device. is that a side effecr?

Name: Anonymous 2009-07-28 16:53

>>9
But the function is not aware of where it's running. It's only a side effect if the function queries where it's running.

Name: Anonymous 2009-07-28 17:11

>>1
It's simple actually, a function could have side-effects during its execution(internally), but still be referetially transparent.

I'll give an example in C, as I don't know what languages you know:


Referentially transparent and pure:
int add1(int x)
{
    return (x+1);
}

Referentially transparent, has side-effects:

int add1(int x)
{
    return ++x; // x is incremented then returned,
                // however it does not affect anything
                // outside the context of this function
}

The second function has internal side-effects, but is referentially transparent.

The book "On Lisp" can show you more examples of this if you know some Lisp.
[/code]

Name: Anonymous 2009-07-28 17:32

>>11
The book "On Lisp" can show you more examples of this if you know some Lisp.
HIBT? paul graham

Name: Anonymous 2009-07-28 17:34

internal side-effects
IHBT

Name: Anonymous 2009-07-28 17:36

IWAT

Name: Anonymous 2009-07-28 17:36

>>8
Are you high? If a function is not altering its environment then it's not causing any effect on that environment, so how the fuck can reading a global variable be a side effect?

Name: Anonymous 2009-07-28 17:40

>>15
YHBT

Name: Anonymous 2009-07-28 17:42

>>15
Okay, you're right.

Name: Anonymous 2009-07-28 17:49

>>7
(newline)
not transparent, with side effects
(newline) always evaluates to #<void>.

Name: Anonymous 2009-07-28 17:53

>>18
It also outputs a newline.

Name: Anonymous 2009-07-28 20:47

>>19
"Impure functions can be included in the expression if their values are discarded and their side effects are insignificant." Whether the newline is significant is a matter of context.

Name: Anonymous 2011-01-31 20:02

<-- check em dubz

Name: Anonymous 2011-02-03 0:28

Name: Anonymous 2013-01-19 21:02

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List