Logo

Programming-Idioms

History of Idiom 76 > diff from v62 to v63

Edit summary for version 63 by cdo:
New Haskell implementation by user [cdo]

Version 62

2019-12-31, 18:10:20

Version 63

2020-04-28, 22:27:43

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Extra Keywords
int radix
Extra Keywords
int radix
Imports
import Data.List
Code
foldl (\acc -> (++ acc) . show) "" . unfoldr (\n -> if n == 0 then Nothing else Just (n `mod` 2, n `div` 2))
Comments bubble
Sure, it will produce the empty string for 0 but it's a catamorphism composed with an anamorphism... I mean how much more Haskell-y do you get? There's even some currying and partial application in there!