Quick actions

cmd+k|ctrl+k

Navigation

Languages

Idris String Replace (Total)

Snippet info

Language

Idris

Visibility

public

Author

mjg.py3

Created

2018-07-30T15:29:14Z

Updated

2018-07-30T15:29:14Z

module Main

total
strReplace : String -> String -> String -> String
strReplace needle replacement = go "" Z (unpack needle) . unpack

where
  go : String -> Nat -> List Char -> List Char -> String
  go acc _ [] text = acc ++ pack text
  go acc _ _ [] = acc
  go acc (S k) needle (x::xs) = go acc k needle xs
  go acc _ (n::ns) (x::xs) =
    if isPrefixOf (n::ns) (x::xs)
    then go (acc ++ replacement) (length ns) (n::ns) xs
    else go (acc ++ cast x) Z (n::ns) xs

main : IO ()
main = putStrLn $ strReplace "foo" "bar" "foo"
INFO