Learn everyday

Simple encoding function

A simple encoding function demonstration in several programming languages.

Donald Le
1 min readSep 5, 2021

--

Photo by Jimmy Dean on Unsplash

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.

Some explanations:

  • 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

Example running

ghci> encode 3 "I love you" 
"L#oryh#|rx"

Scala example

--

--

Donald Le

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.