Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, Haskell has variables. Consider the (essentially equivalent) definitions </p> <pre><code>inc n = n + 1 inc = \n -&gt; n + 1 </code></pre> <p>In both these cases, <code>n</code> is a variable; it will take on different values at different times. The <a href="http://www.haskell.org/onlinereport/" rel="noreferrer">Haskell Report</a>, in <a href="http://www.haskell.org/onlinereport/exps.html" rel="noreferrer">Section 3</a> refers to these explicitly as variables.</p> <p>That <code>n</code> here is a variable may be easier to see if we consider the following complete program:</p> <pre><code>inc n = n + 1 f = inc 0 g = inc 1 main = print (f+g) </code></pre> <p>The answer printed will be "3", of course. When evaluating <code>f</code>, as we expand <code>inc</code> <code>x</code> will take on the value <code>0</code>, and when later (or earlier!) evaluating <code>g</code>, as we expand <code>inc</code> <code>x</code> will take on the value <code>1</code>.</p> <p>Some confusion may have arisen because Haskell, as with the other languages listed in the question, is a single-assignment language: it does not allow the reassignment of variables within a scope. Once <code>n</code> has been assigned the value <code>42</code>, it cannot be anything but 42 without introducing a new scope with a new <code>n</code> (which is a different variable, shadowing the other <code>n</code>) bound to another value.</p> <p>This may not be entirely obvious in some contexts, such as expressions using <code>do</code>:</p> <pre><code> do let n = 1 print n let n = 2 print n </code></pre> <p>but if you remove the syntactic sugar, translating it into Haskell without the <code>do</code>, it becomes clear that there was a new, nested scope created where the <code>n</code> in that inner scope is a different variable that is shadowing the <code>n</code> in the outer scope:</p> <pre><code>(let n = 1 in (print n &gt;&gt; (let n = 2 in print n))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload