Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>-- FoodOne.hs statusMessage :: Int -&gt; String statusMessage 30 = "BBQ ready" statusMessage 40 = "Beverages served" statusMessage rest = "Unknown Food Status" </code></pre> <p>Most compilers are will compile this to a linear search, so it will have linear runtime cost.</p> <pre><code>-- FoodTwo.hs data FoodStatus = FoodStatus { fstatusCode :: Int, fstatusMessage :: String } status30 = FoodStatus 30 "BBQ ready" status40 = FoodStatus 40 "Beverages served" </code></pre> <p>This approach is not comparable to the others: it doesn't provide status lookup given a (dynamically-known) <code>Int</code>. However, for statically-known <code>Int</code>s, it is the fastest: the lookup is done once at compile-time and therefore has constant runtime cost, regardless of how many different constants there are.</p> <pre><code>-- FoodThree.hs statusMessages = [(30,"BBQ ready"),(40,"Beverages served")] </code></pre> <p>Doing naive lookup in this list (e.g. by the built-in <code>lookup</code> function) will involve linear search, and therefore have linear runtime cost.</p> <pre><code>-- FoodFour.hs import Data.Map as M statusMessages = fromList [(30, "BBQ ready"),(40,"Beverages served")] message n = fromMaybe "Unknown Food Status" (M.lookup n statusMessages) </code></pre> <p>The <code>Data.Map</code> module implements balanced search trees, and each lookup takes logarithmic time in the number of different statuses. You might also consider <code>Data.IntMap</code>, which also takes logarithmic time, but has better constants.</p> <pre><code>-- FoodFive.hs import Data.Array as A (statusLo, statusHi) = (30, 40) statusMessages = listArray (statusLo, statusHi) [ Just "BBQ ready" , Nothing , Nothing , {- ... -} , Just "Beverages served" ] message' n = guard (statusLo &lt;= n &amp;&amp; n &lt;= statusHi) &gt;&gt; statusMessages ! n message = fromMaybe "Unknown Food Status" . message' </code></pre> <p>The <code>Data.Array</code> module implements immutable arrays, and each lookup takes constant time. If your array is sparse, however, this may have higher memory costs than the alternatives.</p> <p>These are <em>all</em> the Haskell way. Choose the one with the right tradeoffs between developer annoyance, speed, and memory consumption for <em>you</em>, just as you would in any other language.</p>
    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. 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