Haskell替换字符串中的字符 [英] Haskell replace characters in string

查看:75
本文介绍了Haskell替换字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有字符串"HELLO WORLD",有没有办法我可以调用一个将字符串中的字符"O"替换为字符"X"的函数,以便新字符串看起来像"HELLX WXRLD"?

Supposing I had the string "HELLO WORLD" is there a way I can call a function that replaces the character 'O' in the string with the character 'X' so that the new string would look like "HELLX WXRLD"?

推荐答案

怎么样:

let 
    repl 'o' = 'x'
    repl  c   = c
in  map repl "Hello World"

如果以后需要替换其他字符,只需在repl函数中添加子句.

If you need to replace additional characters later, just add clauses to the repl function.

这篇关于Haskell替换字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆