concept Char in category haskell

appears as: Char, Char
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

Remember that a String is a synonym for a list of Char, so the first two examples share an instance for lists (and this operation is just list concatenation). The third and fourth examples apply instances that represent addition and multiplication—two basic operations over numeric types. They feature newtype wrappers Sum and Product for any type a with an instance of Num a.

The simplify function can now be used as Double -> Integer, Bool -> String, or Char -> String:

Get Programming with Haskell

This is an excerpt from Manning's book Get Programming with Haskell.

The reason for different names for type variables is the same as using different names for regular variables: they may contain different values. In the case of makeTriple, you can imagine a case in which you have a String, a Char, and another String:

Now that you’ve seen how ROT13 works, let’s make a similar cipher for Haskell’s Char type so that you can encode and decode strings. In the preceding example, you used 13 for your rotation because you had 26 characters and you wanted to rotate halfway around all the characters to encode your message. You already encountered a problem when you had to ignore both spaces and special characters in the sentence Jean-Paul likes Simone. Ideally, you could rotate over all N characters for any given character system. What you want is a generic rotN function that can rotate any alphabet system with N elements. Let’s see how to create a simple four-letter alphabet that you can use to experiment with.

Now you can use rotN to rotate Chars. The only thing you need to do is to figure out how many Chars there are. You can start by using the maxBound value required by the Bounded type class. By using maxBound, you can get the largest Char value. Then you can convert this to an Int by using the fromEnum function required by Enum. Here’s the code to get the number of the largest Char.

Listing 15.3. Getting the number representing the largest Char
largestCharNumber :: Int largestCharNumber = fromEnum (maxBound :: Char) #1 #1 Notice that you must add :: Char so maxBound knows which type you’re using. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":59},{\"line\":1,\"ch\":61}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}]]"} !@%STYLE%@!

But the Int value for the lowest Char is 0 (you can get this using the same trick with (minBound). Because of this, the size of the Char alphabet is largestCharNumber + 1. If you look up the Enum type class on Hackage, you’ll find that the definition does assume that Enums start at 0 and go to n–1 (recall that Hackage, https://hackage.haskell.org/, is the online source for full definitions of Haskell type classes). Because of this, it’s generally safe for you to assume that the total number of items in any alphabet is always maxBound + 1. If you wanted to write a Char-specific rotN function, it would look like the following.

Listing 15.4. Rotating a single Char
rotChar :: Char -> Char rotChar charToEncrypt = rotN sizeOfAlphabet charToEncrypt where sizeOfAlphabet = 1 + fromEnum (maxBound :: Char)

You can test this if you create an instance of the OneTimePad data type. But what should you use for the pad? As long as it’s longer than your plain text, you should be fine, but how can you get something long enough for any possible text you could input? You can solve this by using lazy evaluation to create an infinite list of all Chars cycling forever!