Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is just a variant of @ony's, but how I'd write it:</p> <pre><code>import Data.List (unfoldr) digits :: (Integral a) =&gt; a -&gt; [a] digits = unfoldr step . abs where step n = if n==0 then Nothing else let (q,r)=n`divMod`10 in Just (r,q) </code></pre> <p>This will product the digits from low to high, which while unnatural for reading, is generally what you want for mathematical problems involving the digits of a number. (<a href="http://projecteuler.net/" rel="noreferrer">Project Euler</a> anyone?) Also note that <code>0</code> produces <code>[]</code>, and negative numbers are accepted, but produce the digits of the absolute value. (I don't want partial functions!)</p> <p>If, on the other hand, I need the digits of a number as they are commonly written, then I would use @newacct's method, since the problem <em>is</em> one of essentially orthography, not math:</p> <pre><code>import Data.Char (digitToInt) writtenDigits :: (Integral a) =&gt; a -&gt; [a] writtenDigits = map (fromIntegral.digitToInt) . show . abs </code></pre> <p>Compare output:</p> <pre><code>&gt; digits 123 [3,2,1] &gt; writtenDigits 123 [1,2,3] &gt; digits 12300 [0,0,3,2,1] &gt; writtenDigits 12300 [1,2,3,0,0] &gt; digits 0 [] &gt; writtenDigits 0 [0] </code></pre> <p>In doing <a href="http://projecteuler.net/" rel="noreferrer">Project Euler</a>, I've actually found that some problems call for one, and some call for the other.</p> <h2>About <code>.</code> and "point-free" style</h2> <p>To make this clear for those not familiar with Haskell's <code>.</code> operator, and "point-free" style, these could be rewritten as:</p> <pre><code>import Data.Char (digitToInt) import Data.List (unfoldr) digits :: (Integral a) =&gt; a -&gt; [a] digits i = unfoldr step (abs i) where step n = if n==0 then Nothing else let (q,r)=n`divMod`10 in Just (r,q) writtenDigits :: (Integral a) =&gt; a -&gt; [a] writtenDigits i = map (fromIntegral.digitToInt) (show (abs i)) </code></pre> <p>These are exactly the same as the above. You should learn that these are the same:</p> <pre><code>f . g (\a -&gt; f (g a)) </code></pre> <p>And "point-free" means that these are the same:</p> <pre><code>foo a = bar a foo = bar </code></pre> <p>Combining these ideas, these are the same:</p> <pre><code>foo a = bar (baz a) foo a = (bar . baz) a foo = bar . baz </code></pre> <p>The laster is idiomatic Haskell, since once you get used to reading it, you can see that it is very concise.</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. 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