Skip to main content

Strings

caution

The documentation that you're reading is a design document where most of the features you're reading are yet to be implemented. Check the Note on the Docs

NeoHaskell embraces strings as a versatile and indispensable part of programming, especially when dealing with data that is inherently variable and not known until runtime.

Crafting and Utilizing Strings

Defining a string in NeoHaskell is as simple as in any language—just a matter of enclosing your text within double quotes:

greeting :: String
greeting = "Hello, NeoHaskell!"

Purposeful Use of Strings

While strings are powerful, they should be used judiciously. In NeoHaskell, like in any language that supports strong typing, strings are ideal for representing text values that are inherently unpredictable or user-defined, such as names, email addresses, or free-form text input.

userName :: String
userName = "Jesse123"

userEmail :: String
userEmail = "[email protected]"

Enums vs. Strings

For values that are known and finite, enums are a more type-safe option than strings. Using enums can prevent errors like typos at compile time rather than at runtime, making your code more reliable and easier to maintain.

data UserRole
= Admin
| User
| Guest

-- Correct use of enums for known, finite values
assignRole :: String -> UserRole
assignRole roleStr =
case roleStr of
"admin" ->
Admin

"user" ->
User

_ ->
Guest

Embracing Type Safety

NeoHaskell encourages embracing type safety by using strings only when necessary and opting for enums or other more specific types when possible. This practice aids in avoiding common pitfalls associated with string manipulation, such as unexpected mutations or case sensitivity issues.

String Operations

NeoHaskell provides a comprehensive set of functions for string manipulation, allowing you to perform common operations such as concatenation, case conversion, and more, without the overhead of object-oriented methods.

import String

-- Joining strings together
combinedString :: String
combinedString = String.concat ["Neo", "Haskell"]

-- Changing to uppercase
uppercaseString :: String
uppercaseString = String.toUpper "NeoHaskell"

-- Replacing text within a string
replacedText :: String
replacedText str = String.replace "Old" "Neo" "OldHaskell"

Interacting with Strings

In NeoHaskell, strings are not merely static entities but dynamic constructs that can be inspected, dissected, and transformed, providing robust capabilities for developers to handle text data.

Conclusion and Next Steps

Strings are a fundamental tool in your NeoHaskell toolkit, to be used when the situation demands flexibility and dynamism. As you grow in your NeoHaskell journey, understanding when to use strings and when to opt for more rigid types like enums will be key to writing clean, effective code.