- Haskell Data Analysis Cookbook
- Nishant Shukla
- 322字
- 2021-12-08 12:43:35
Trimming excess whitespace
The text obtained from sources may unintentionally include beginning or trailing whitespace characters. When parsing such an input, it is often wise to trim the text. For example, when Haskell source code contains trailing whitespace, the GHC compiler ignores it through a process called lexing. The lexer produces a sequence of tokens, effectively ignoring meaningless characters such as excess whitespace.
In this recipe, we will use built-in libraries to make our own trim
function.
How to do it...
Create a new file, which we will call Main.hs
, and perform the following steps:
- Import the
isSpace :: Char -> Bool
function from the built-inData.Char
package:import Data.Char (isSpace)
- Write a trim function that removes the beginning and trailing whitespace:
trim :: String -> String trim = f . f where f = reverse . dropWhile isSpace
- Test it out within
main
:main :: IO () main = putStrLn $ trim " wahoowa! "
- Running the code will result in the following trimmed string:
$ runhaskell Main.hs wahoowa!
How it works...
Our trim
function lazily strips the whitespace from the beginning and ending parts of the string. It starts by dropping whitespace letters from the beginning. Then, it reverses the string to apply the same function again. Finally, it reverses the string one last time to bring it back to the original form. Fortunately, the isSpace
function from Data.Char
handles any Unicode space character as well as the control characters \t
, \n
, \r
, \f
, and \v
.
There's more…
Ready-made parser combinator libraries such as parsec
or uu-parsinglib
could be used to do this instead, rather than reinventing the wheel. By introducing a Token
type and parsing to this type, we can elegantly ignore the whitespace. Alternatively, we can use the alex lexing library (package name, alex
) for this task. These libraries are overkill for this simple task, but they allow us to perform a more generalized tokenizing of text.
- The Supervised Learning Workshop
- OpenDaylight Cookbook
- ASP.NET MVC4框架揭秘
- PHP基礎(chǔ)案例教程
- 實(shí)戰(zhàn)Java程序設(shè)計(jì)
- Haxe Game Development Essentials
- Apache Kafka Quick Start Guide
- 基于Struts、Hibernate、Spring架構(gòu)的Web應(yīng)用開(kāi)發(fā)
- Serverless computing in Azure with .NET
- Learning Node.js for .NET Developers
- 快速入門(mén)與進(jìn)階:Creo 4·0全實(shí)例精講
- 零基礎(chǔ)學(xué)C++(升級(jí)版)
- WordPress Search Engine Optimization(Second Edition)
- Pandas 1.x Cookbook
- Design Patterns and Best Practices in Java