How to resolve the algorithm Morse code step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Morse code step by step in the F# programming language

Table of Contents

Problem Statement

Morse code is one of the simplest and most versatile methods of telecommunication in existence. It has been in use for more than 175 years — longer than any other electronic encoding system.

Send a string as audible Morse code to an audio device   (e.g., the PC speaker).

As the standard Morse code does not contain all possible characters, you may either ignore unknown characters in the file, or indicate them somehow   (e.g. with a different pitch).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Morse code step by step in the F# programming language

Source code in the fsharp programming language

open System
open System.Threading

let morse = Map.ofList
                [('a', "._ "); ('b', "_... "); ('c', "_._. "); ('d', "_.. ");
                ('e', ". "); ('f', ".._. "); ('g', "__. "); ('h', ".... ");
                ('i', ".. "); ('j', ".___ "); ('k', "_._ "); ('l', "._.. ");
                ('m', "__ "); ('n', "_. "); ('o', "___ "); ('p', ".__. ");
                ('q', "__._ "); ('r', "._. "); ('s', "... "); ('t', "_ ");
                ('u', ".._ "); ('v', "..._ "); ('w', ".__ "); ('x', "_.._ ");
                ('y', "_.__ "); ('z', "__.. "); ('0', "_____ "); ('1', ".____ ");
                ('2', "..___ "); ('3', "...__ "); ('4', "...._ "); ('5', "..... ");
                ('6', "_.... "); ('7', "__... "); ('8', "___.. "); ('9', "____. ")]

let beep c =
    match c with
    | '.' ->
        printf "."
        Console.Beep(1200, 250)
    | '_' ->
        printf "_"
        Console.Beep(1200, 1000)
    | _ ->
        printf " "
        Thread.Sleep(125)

let trim (s: string) = s.Trim()
let toMorse c = Map.find c morse
let lower (s: string) = s.ToLower()
let sanitize = String.filter Char.IsLetterOrDigit

let send = sanitize >> lower >> String.collect toMorse >> trim >> String.iter beep

send "Rosetta Code"


  

You may also check:How to resolve the algorithm Function composition step by step in the Order programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the 11l programming language
You may also check:How to resolve the algorithm Stack traces step by step in the Ruby programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Maxima programming language
You may also check:How to resolve the algorithm Memory layout of a data structure step by step in the Java programming language