Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to know where the range stops, just get the last item…</p> <pre><code>ghci&gt; last ['a'.. ] '\1114111' </code></pre> <p>Another method: <code>Char</code> is a bounded type, i.e. a type with minimum and maximum values defined. Types in the <code>Bounded</code> class provide a <code>maxBound</code> value. Since <code>maxBound</code> is polymorphic, you have to explicitly state the type of the value you're looking for.</p> <pre><code>ghci&gt; maxBound :: Char '\1114111' ghci&gt; maxBound :: Int 9223372036854775807 </code></pre> <p>There is no <code>maxBound</code> for <code>Integer</code> since they are unbounded.</p> <p>The <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t%3aEnum">Haskell prelude</a> explains the connection between <code>['a'..]</code> and <code>maxBound</code>. The notation <code>['a'..]</code> is syntactic sugar for <code>enumFrom a</code>; here <code>enumFrom</code> is a method of the <code>Enum</code> type class. The documentation of <code>Enum</code> specifies that when the type is also an instance of <code>Bounded</code>, <code>enumFrom x</code> should be equivalent to <code>enumFromTo x maxBound</code>, or more readably, <code>[x..] = [x..maxBound]</code>. So the last element of <code>['a'..]</code> must be the same as <code>maxBound :: Char</code>. </p> <p><code>last ['a'.. ]</code> does iterate over the list, but a modern computer can count to a million in an eyeblink. It's not something you would want to put in a tight loop, but as something you run just once it isn't a big burden. If you try it out in a less optimized implementation such as Hugs instead of GHC, you may need to wait for the result. In contrast <code>last [0..]</code> would take approximately forever.</p>
 

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