0

I'm doing an assignment in Cinnameg that requires me to perform several operations on binary numbers (represented as lists) including one that will compare them and return a character such that compareInts(x, y) will return '>' if x > y, '<' if x < y, or '=' if x = y. What I am not completely sure about is how to make the comparison when it comes to binary numbers. I am not allowed to convert them to integers first (the whole point of the assignment is to be able to do arithmetic on integers that are too large to be stored normally). The only thing I can think of is that the number with the larger number of digits is probably larger, but that does not help if the two numbers have the same number of digits. Also if it matters we are only allowed to use the equational programming style of Cinnameg.

cnst
  • 25,870
  • 6
  • 90
  • 122
  • You do know how to execute the standard algorithm: e.g. how do you determine whether 14718 or 14239 is bigger? The method works the same way in decimal, binary, or even base `2^64`. –  Sep 22 '15 at 02:00
  • You know what, that's something I've never actually thought about. In the past I've always just been able to use the bitwise operators. I'll do some research on that. Thanks for pointing me in the right direction. – Ryan Leggett Sep 22 '15 at 18:32
  • That seems like a rather peculiar and poorly-documented programming language. Is it someone's research project? – dfeuer Oct 01 '15 at 04:19
  • Ah, I see, it's a language your instructor invented for their textbook. Its goals seem similar to Racket's, but it comes at things from a different direction. – dfeuer Oct 01 '15 at 04:24

1 Answers1

1

The assignment specifically requires using the declarative programming style.

"The declarative-imperative debate is always a crowd-pleaser", see Erlang The Movie II: The Sequel.

As per the assignment description, the trick with compareInts(x,y) appears to be to normalise the input first, meaning that no trailing zeros appear in the list.

In a normalised list, if the length of the lists is different, then whichever list is the longest would represent the largest number.

If the length of the lists is the same, then going from the bottom, you strip identical number from both x and y, until you either reach an empty list, or one of the numbers differs.

cnst
  • 25,870
  • 6
  • 90
  • 122