3

So I'm using a Grey-code generator to generate all possible bit Strings of length 6. The generator is as follows:

gray :: Integer -> String
gray n
 | n == 0 = [""]
 | n >  0 = map (++"0") (gray (n-1)) ++
            map (++"1") (reverse (gray (n-1)))

recipes = gray 6

Then, I'm attempting to get a specific bit from each of these Strings and convert that bit to an Integer. I'm doing this in the following way:

cost' :: String -> Cost
cost' r i = toInteger( ord ( r!!i ) )

Now, for some reason this isn't working. Regardless of what 'i' value I use, the function cost' will either result in 48 (if the bit in position 2 of the list is 0 --> ex. '100000') or 49 (if the bit in position 2 of the list is 1 --> ex. '101000').

It doesn't make any sense to me why this is.. It's my understanding that Strings are stored as lists in Haskell, and that to get a certain element 'i' from a list 'r' in Haskell, you execute 'r!!i'.

1 Answer 1

5

That's because ord returns the code point number of the character, and '0' is code point 48, '1' is code point 49. The function you want to use is digitToInt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.