Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Is crossfoot exactly what you want to do, or is it just summing the digits of a long number?</p> <p>because if you just want to sum the digits, then:</p> <pre><code>let crossfoot (x:'a) = x.ToString().ToCharArray() |&gt; (Array.fold(fun acc x' -&gt; if x' &lt;&gt; '.' then acc + (int x') else acc) 0) </code></pre> <p>... And you are done. </p> <p>Anyways, Can you convert stuff to a string, drop the decimal point, remember where the decimal point is, interpret it as an int, run crossfoot?</p> <p>Here is my solution. I am not sure exactly how you want "crossfoot" to work when you have a decimal point added.</p> <p>For instance, do you want: <code>crossfoot(123.1) = 7</code> or <code>crossfoot(123.1) = 6.1</code>? (I'm assuming you want the latter)</p> <p>Anyways, the code does allow you to work with numbers as generics. </p> <pre><code>let crossfoot (n:'a) = // Completely generic input let rec crossfoot' (a:int) = // Standard integer crossfoot if a = 0 then 0 else a%10 + crossfoot' (a / 10) let nstr = n.ToString() let nn = nstr.Split([|'.'|]) // Assuming your main constraint is float/int let n',n_ = if nn.Length &gt; 1 then nn.[0],nn.[1] else nn.[0],"0" let n'',n_' = crossfoot'(int n'),crossfoot'(int n_) match n_' with | 0 -&gt; string n'' | _ -&gt; (string n'')+"."+(string n_') </code></pre> <p>If you need to input big integers or int64 stuff, the way crossfoot works, you can just split the big number into bitesize chunks (strings) and feed them into this function, and add them together.</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. 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