A simple haskell string controls

Theres a little problem I want to solve with Haskell: replace a function that changes all characters in a string for one specific parameter. The function has a de-signature:

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)"
+3
source share
4 answers

You can 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
+6
source

For such cases, you can use the text-format-simple library :

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

(Text.Regex.Posix) search-replace /\Wx\W/ ( Perl). x 6.2 x + quux.

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

For an extra hard core, you can parse your expression as an AST and perform a replacement at that level.

+1
source

Source: https://habr.com/ru/post/1696884/


All Articles