Tuesday, 7 June 2016

Haskell: get the unicode distance between two characters


Sample.hs
import Data.Char

offset :: Char -> Char -> Int
offset c1 c2 = abs (ord c1 - ord c2)

Prelude> :load Sample.hs
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> offset 'a' 'b'
1
*Main> offset 'a' 'z'
25
*Main> offset 'n' 'a'
13
*Main> offset '*' 'a'
55


Previous                                                 Next                                                 Home

No comments:

Post a Comment