<html><body><div style="color:#000; background-color:#fff; font-family:Courier New, courier, monaco, monospace, sans-serif;font-size:10pt"><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 10pt; ">Can the following encode and decode functions be written without any library calls from modules 'lists' and 'io_lib'? Please show how. The test cases are included at the end. Thanks</div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 10pt; "><br></div><div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">$ cat bcd.erl</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">-module(bcd).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">-compile(export_all).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">% pack the digits
of an integer as BCD in a given size of binary</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">% pad with leading zeros</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">encode(N, Size) -></font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2"> << <<X:4>> || X <- lists:flatten(io_lib:fwrite("~*..0B", [Size*2, N])) >>.</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">% unpack the given size of BCD binary into an integer</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">% strip leading zeros</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">decode(N, Size) -></font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">
io_lib:fread("~d", [ X+$0 || <<X:4>> <= <<N:(Size*8)/bits>> ]).</font></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 10pt; "><br></div><div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">$ erl</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">Eshell V5.8.3 (abort with ^G)</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">1> c(bcd).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">{ok,bcd}</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">2> bcd:encode(1, 5).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2"><<0,0,0,0,1>></font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif"
size="2">3> bcd:decode(v(2), 5).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">{ok,[1],[]}</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">4> bcd:encode(12345, 5).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2"><<0,0,1,35,69>></font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">5> bcd:decode(v(4), 5).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">{ok,[12345],[]}</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">6> bcd:encode(1234567890, 5).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2"><<18,52,86,120,144>></font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">7>
bcd:decode(v(6), 5).</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">{ok,[1234567890],[]}</font></div><div><font face="'Courier New', courier, monaco, monospace, sans-serif" size="2">8> </font></div></div></div></div></body></html>