Exercise Diamon: What is a Letter in Erlang?

Hello,

in the Diamond-Exercise the predefined Function looks like this:

rows(Letter) -> undefined.

So, I expected a single Letter, a Char, something like $A. But to my surprise the tests calling the function with a String “A”.

?_assertEqual(Expected, diamond:rows("A"))

This raises for me the question: How single letters are handled in Erlang? Are they handled as Numbers like $A or as Lists like “A”?

Regards,
Eisenaxt

I don’t know Erlang, but for this particular exercise, the tests indicate that single char strings are used to represent letters. That’s how they’ve chosen to represent them when writing the tests, and that’s perfectly fine.

Note that using strings for letters comes directly from the problem specs, but each track is free to use whatever they believe it is more appropriate. For example, in Clojure we use characters (\A) instead of strings (“A”).

A single letter is really a “codepoint” or integer which represents the letter. the string notation, “…”, is a shortcut for a list of codepoints, so things like:

WordAsString = "Hello"
WordAsChars = [$H, $e, $l, $l, $o]
WordAsCodePoints = [72, 101, 108, 108, 111]

are all equivalent. The $ prefix is really just to make it easier than working with raw numbers, but it is an integer and not a separate datatype.

1 Like