Simple haskell string manage

Tags: haskell string

Theres is a little problem I want to solve with Haskell: let substitute a function that change all of the wildcards in a string for one concrete parameter. The function has de signature of:

subs :: String -> String -> String -> String
-- example:
-- subs 'x' "x^3 + x + sin(x)" "6.2" will generate
--          "6.2^3 + 6.2 + sin(6.2)"
By : paurullan


You could use the Text.Regex package.

Your example might look something like this:

import Text.Regex(mkRegex, subRegex)

subs :: String -> String -> String -> String
subs wildcard input value = subRegex (mkRegex wildcard) input value
By : Dan Dyer

You can use text-format-simple library for such cases:

import Text.Format
format "{0}^3 + {0} + sin({0})" ["6.2"]

Use regular expressions (Text.Regex.Posix) and search-replace for /\Wx\W/ (Perl notation). Simply replacing x to 6.2 will bring you trouble with x + quux.

Haskell Regex Replace for more information (I think this should be imported to SO.

For extra hard-core you could parse your expression as AST and do the replacement on that level.

By : squadette

This video can help you solving your question :)
By: admin