Skip to content

Instantly share code, notes, and snippets.

@talhaahussain
Created June 16, 2024 05:33
Show Gist options
  • Save talhaahussain/c2d303910cfaae08b630a3027a52b519 to your computer and use it in GitHub Desktop.
Save talhaahussain/c2d303910cfaae08b630a3027a52b519 to your computer and use it in GitHub Desktop.
charToMorse.hs -- A Haskell function to convert a given character to its equivalent Morse code string.
charToMorse :: Char -> [Char]
charToMorse x
| x == ' ' = " " -- U+0020
| x == '!' = "-.-.--" -- U+0021
| x == '"' = ".-..-." -- U+0022
| x == '&' = ".-..." -- U+0026
| x == '\'' = ".----." -- U+0027
| x == '(' = "-.--." -- U+0028
| x == ')' = "-.--.-" -- U+0029
| x == '+' = ".-.-." -- U+002B
| x == ',' = "--..--" -- U+002C
| x == '-' = "-....-" -- U+002D
| x == '.' = ".-.-.-" -- U+002E
| x == '/' = "-..-." -- U+002F
| x == '0' = "-----" -- U+OO30
| x == '1' = ".----" -- U+0031
| x == '2' = "..---" -- U+0032
| x == '3' = "...--" -- U+0033
| x == '4' = "....-" -- U+0034
| x == '5' = "....." -- U+0035
| x == '6' = "-...." -- U+0036
| x == '7' = "--..." -- U+0037
| x == '8' = "---.." -- U+0038
| x == '9' = "----." -- U+0039
| x == ':' = "---..." -- U+003A
| x == '=' = "-...-" -- U+003D
| x == '?' = "..--.." -- U+003F
| x == '@' = ".--.-." -- U+0040
| x == 'A' || x == 'a' = ".-" -- U+0041 / U+0061
| x == 'B' || x == 'b' = "-..." -- U+0042 / U+0062
| x == 'C' || x == 'c' = "-.-." -- U+0043 / U+0063
| x == 'D' || x == 'd' = "-.." -- U+0044 / U+0064
| x == 'E' || x == 'e' = "." -- U+0045 / U+0065
| x == 'F' || x == 'f' = "..-." -- U+0046 / U+0066
| x == 'G' || x == 'g' = "--." -- U+0047 / U+0067
| x == 'H' || x == 'h' = "...." -- U+0048 / U+0068
| x == 'I' || x == 'i' = ".." -- U+0049 / U+0069
| x == 'J' || x == 'j' = ".---" -- U+004A / U+006A
| x == 'K' || x == 'k' = "-.-" -- U+004B / U+006B
| x == 'L' || x == 'l' = ".-.." -- U+004C / U+006C
| x == 'M' || x == 'm' = "--" -- U+004D / U+006D
| x == 'N' || x == 'n' = "-." -- U+004E / U+006E
| x == 'O' || x == 'o' = "---" -- U+004F / U+006F
| x == 'P' || x == 'p' = ".--." -- U+0050 / U+0070
| x == 'Q' || x == 'q' = "--.-" -- U+0051 / U+0071
| x == 'R' || x == 'r' = ".-." -- U+0052 / U+0072
| x == 'S' || x == 's' = "..." -- U+0053 / U+0073
| x == 'T' || x == 't' = "-" -- U+0054 / U+0074
| x == 'U' || x == 'u' = "..-" -- U+0055 / U+0075
| x == 'V' || x == 'v' = "...-" -- U+0056 / U+0076
| x == 'W' || x == 'w' = ".--" -- U+0057 / U+0077
| x == 'X' || x == 'x' = "-..-" -- U+0058 / U+0078
| x == 'Y' || x == 'y' = "-.--" -- U+0059 / U+0079
| x == 'Z' || x == 'z' = "--.." -- U+005A / U+007A
| x == '_' = "..--.-" -- U+005F
| otherwise = "#" -- undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment