Simple encoding function
A simple encoding function demonstration in several programming languages.
So while I’m learning Haskell, I would try to write a simple encoding function then decode it back. Then add some more examples in other languages such as Scala, or Python.
- ord function show us the offset of the character in Unicode
- chr function convert from offset to actually character
- The function take an offset integer, and the needed encode string to convert
ghci> encode 3 "I love you"
We define the string we want to encode, then we iterate each character in the string and convert to character with index is +3.
Running this show the result the same
And the last is example for Python
Running this show encoded string
Happy learning ~~
Learn you a great haskell
Python Programming/Text - Wikibooks, open books for an open world
To get the length of a string, we use the len() function: >>> len("Hello Wikibooks!") 16 You can slice strings just…