Anonymous user
Binary digits: Difference between revisions
→{{header|F Sharp|F#}}: added method to use with "%a" which is a specific technique for F# and not available directly in C#, for instance
No edit summary |
(→{{header|F Sharp|F#}}: added method to use with "%a" which is a specific technique for F# and not available directly in C#, for instance) |
||
Line 1,144:
=={{header|F Sharp|F#}}==
<lang FSharp>open System
for i in [5; 50; 9000] do printfn "%s" <| Convert.ToString (i, 2)</lang>
<lang FSharp>open System
Line 1,159:
[5; 50; 9000]
|> List.iter printBin</lang>
Or more idiomatic so that you can use it with any printf-style function and the <code>%a</code> format specifier (most flexible):
<lang FSharp>open System
open System.IO
// define a callback function for %a
let bin (tw: TextWriter) value =
tw.Write("{0}", Convert.ToString(int64 value, 2))
// use it with printfn with %a
[5; 50; 9000]
|> List.iter (printfn "binary: %a" bin)</lang>
Output (either version):
<pre>
|