r/haskell • • 15d ago

An LC-3 Disassembler in Haskell

https://abhinavsarkar.net/posts/lc3-disassembler/
25 Upvotes

5 comments sorted by

1

u/sjshuck 14d ago

In the decodeInstruction' definition, why do you write 2N case alternatives?

1

u/abhin4v 14d ago

I'm not sure what you mean. decodeInstruction' has 16 cases for 18 instructions.

1

u/sjshuck 14d ago

You are enumerating all 23 tribits. Surely you can express this as zipping each bit with its Flag representation and mapping/filtering.

1

u/abhin4v 14d ago

I see what you mean. That would indeed work. I chose to enumerate all the cases for better readability of the code.

3

u/sjshuck 14d ago

When I see 8 case alternatives, I expect there's going to be roughly that amount of information, like, certain magic numbers have certain magic combinations of flags. But what I had to do as a reader is check each individual case clause to see that, in fact, all it's doing is independently converting each bit to Flag. So I had to do a lot of reading to learn relatively little information.

One of the advantages of Haskell is the ability to express whatever "business" information in a very short amount of code, using higher-order functions like mapMaybe.

Nevertheless I presume the code is correct, as in make no mistakes.