Base58Check encoding: Difference between revisions
m
→{{header|Haskell}}: Tidying
m (→version 3: aligned comment with assignment value.) |
m (→{{header|Haskell}}: Tidying) |
||
Line 417:
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists:
{{Trans|Python}}
<lang Haskell>import Data.Array (Array, listArray, (!)
import Numeric (showHex, showIntAtBase)
------------------- BASE58CHECK ENCODING -----------------
base58Encode▼
:: (Integral a, Show a)▼
▲base58Encode ::
=> a -> String▼
a ->
String
base58Encode =
baseEncode $
listArray (0, 57) $
('A', 'H'),
uncurry enumFromTo▼
('J', 'N'),
('P', 'Z'),
('a', 'k'),
('m', 'z')
▲ >>= uncurry enumFromTo
baseEncode ::
a ->
baseEncode cs n = showIntAtBase (fromIntegral $ length cs) (cs !) n []▼
String
baseEncode cs n =
showIntAtBase
(cs !)
n
[]
-
main :: IO ()
main =
putStrLn $
fTable
"Base 58 encoding:\n"
--(\x -> '0' : 'x' : showHex x [])
(("0x" <>) . flip showHex [])
id
[ 25420294593250030202636073700053352635053786165627414518,
▲ , 0x61
0x636363,
]
-
fTable ::
String ->
(b -> String) ->
(a -> b) ->
[a] ->
String
fTable s xShow fxShow f xs =
let w = maximum $ fmap length (xShow <$> xs)
rjust n c
in unlines $
s :
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang>▼
fmap
( ((<>) . rjust w ' ' . xShow)
)
xs</lang>
{{Out}}
<pre>Base 58 encoding:
|