Skip to content

Commit

Permalink
Reto mouredev#19 - haskell"
Browse files Browse the repository at this point in the history
  • Loading branch information
admelix committed May 11, 2023
1 parent c1c5398 commit 48f3148
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/haskell/admelix.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Data.List (maximumBy)
import Data.Char (isAlpha, isSpace)
import Data.Ord (comparing)

analyzeText :: String -> (Int, Double, Int, String)
analyzeText text =
let (wordCount, totalLength, sentenceCount, longestWord) = analyzeTextHelper text 0 0 0 0 0 "" "" 0
averageLength = fromIntegral totalLength / fromIntegral wordCount
in (wordCount, averageLength, sentenceCount, longestWord)

analyzeTextHelper :: String -> Int -> Int -> Int -> Int -> Int -> String -> String -> Int -> (Int, Int, Int, String)
analyzeTextHelper [] wordCount totalLength sentenceCount longestLength currentLength currentWord longestWord =
(wordCount, totalLength, sentenceCount, longestWord)
analyzeTextHelper (c:cs) wordCount totalLength sentenceCount longestLength currentLength currentWord longestWord
| isAlpha c =
analyzeTextHelper cs wordCount totalLength sentenceCount longestLength (currentLength + 1) (currentWord ++ [c]) longestWord
| isSpace c =
let newWordCount = if currentLength > 0 then wordCount + 1 else wordCount
newTotalLength = totalLength + currentLength
newLongestLength = if currentLength > longestLength then currentLength else longestLength
newLongestWord = if currentLength > longestLength then currentWord else longestWord
in analyzeTextHelper cs newWordCount newTotalLength sentenceCount newLongestLength 0 "" newLongestWord
| c == '.' =
analyzeTextHelper cs wordCount totalLength (sentenceCount + 1) longestLength currentLength currentWord longestWord
| otherwise =
analyzeTextHelper cs wordCount totalLength sentenceCount longestLength currentLength currentWord longestWord

main :: IO ()
main = do
putStrLn "Ingrese un texto:"
text <- getLine
let (wordCount, averageLength, sentenceCount, longestWord) = analyzeText text
putStrLn $ "Número total de palabras: " ++ show wordCount
putStrLn $ "Longitud media de las palabras: " ++ show averageLength
putStrLn $ "Número de oraciones: " ++ show sentenceCount
putStrLn $ "Palabra más larga: " ++ longestWord

git config --global user.email "[email protected]" && git config --global user.name "Jose Sakuda"

0 comments on commit 48f3148

Please sign in to comment.