How to resolve the algorithm Palindrome detection step by step in the Elm programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Palindrome detection step by step in the Elm programming language

Table of Contents

Problem Statement

A palindrome is a phrase which reads the same backward and forward. Write a function or program that checks whether a given sequence of characters (or, if you prefer, bytes) is a palindrome. For extra credit:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Palindrome detection step by step in the Elm programming language

Source code in the elm programming language

import String exposing (reverse, length) 
import Html exposing (Html, Attribute, text, div, input)
import Html.Attributes exposing (placeholder, value, style)
import Html.Events exposing (on, targetValue)
import Html.App exposing (beginnerProgram)

-- The following function (copied from Haskell) satisfies the 
-- rosettacode task description.
is_palindrome x = x == reverse x

-- The remainder of the code demonstrates the use of the function 
-- in a complete Elm program.
main = beginnerProgram { model = "" , view = view , update = update }

update newStr oldStr = newStr

view : String -> Html String
view candidate =
  div []
    ([ input
        [ placeholder "Enter a string to check."
        , value candidate
        , on "input" targetValue 
        , myStyle
        ]
        []
     ] ++ 
     [ let testResult = 
             is_palindrome candidate

           statement = 
             if testResult then "PALINDROME!" else "not a palindrome"

       in div [ myStyle] [text statement]
     ])

myStyle : Attribute msg
myStyle =
  style
    [ ("width", "100%")
    , ("height", "20px")
    , ("padding", "5px 0 0 5px")
    , ("font-size", "1em")
    , ("text-align", "left")
    ]


  

You may also check:How to resolve the algorithm Hilbert curve step by step in the Factor programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Julia programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the Nim programming language
You may also check:How to resolve the algorithm Execute Computer/Zero step by step in the BASIC programming language
You may also check:How to resolve the algorithm Dice game probabilities step by step in the BASIC programming language