typestar

Composición y pipelines en Haskell

Cuenta palabras de dos maneras y pone lado a lado el estilo point-free y el estilo con argumentos.

-- (.) glues functions right to left; ($) applies one without parens.
module WordStats where

import Data.Char (isAlpha, toLower)

normalize :: String -> String
normalize = map toLower . filter (\c -> isAlpha c || c == ' ')

-- Point-free: the text argument is never named at all.
wordCount :: String -> Int
wordCount = length . words . normalize

-- The same pipeline written pointed, with ($) in place of nesting.
wordCountPointed :: String -> Int
wordCountPointed text = length $ words $ normalize text

longest :: String -> Int
longest = maximum . (0 :) . map length . words . normalize

Cómo funciona

  1. normalize compone map toLower con un filter que descarta la puntuación.
  2. wordCount = length . words . normalize no nombra su argumento en ningún momento.
  3. wordCountPointed text = length $ words $ normalize text hace el mismo trabajo con ($).

Palabras clave y builtins usados aquí

El intento, en números

Líneas
18
Caracteres a escribir
590
Tokens
81
Ritmo de tres estrellas
70 tpm

Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 69 segundos.

Escribe este fragmento

Paso 3 de 3 en Funciones; paso 6 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →