Skip to content

Commit 94df093

Browse files
committed
Complete exercise 7.5.
1 parent b476a5b commit 94df093

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

chapter_7/exercise_5/reverse_bits.erl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-module(reverse_bits).
2+
3+
-export([reverse/1]).
4+
5+
% We take a binary and then need to reverse all the bits.
6+
reverse(Binary) ->
7+
% Call the recursive function reverse/2 with the binary and an empty binary
8+
% as the accumlator.
9+
reverse(Binary, <<>>).
10+
11+
reverse(<<>>, Result) ->
12+
% Reverse the binary until we get to the end, then return the accumlator
13+
Result;
14+
reverse(<<Bit:1, Bin/bitstring>>, Result) ->
15+
% Take the first bit off the front of the binary and push it onto the
16+
% accumlator binary. Call the function again with the remaining bits
17+
reverse(Bin, <<Bit:1, Result/bitstring>>).

0 commit comments

Comments
 (0)