Reputation: 3151
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'.
Upvotes: 3
Views: 359
Reputation: 183878
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
.
Upvotes: 5