Haskell - 在字符串中的指定位置将Char转换为另一个指定的Char [英] Haskell -Changing a Char to another specified Char in a specified position in a String
问题描述
- 我一直在学习一些Haskell,并提出了一个解决方案来解决我的一个练习问题。 >在字符串中的指定位置将Char更改为另一个指定的Char
changeStr :: Int - >字符 - >字符串 - > String
changeStr x char zs =(take(x-1)zs)++ [(changeChar(head(take x zs))char)] ++(drop x zs)
- 将Char更改为另一个Char
changeChar :: Char - >字符 - > Char
changeChar xy = y
我只是想问,是否有其他方式我可以用更简单的方法使用不同的方法来做到这一点吗?
> changeChar 。它实际上非常接近一个非常常见的Haskell Prelude
函数,称为 const
。要获得 changeChar
,我们只需要 flip const
。
const :: a - > b - > a
const a b = a
changeChar :: Char - >字符 - > Char
changeChar =翻转常量
- =翻转(\ a _ - > a)
- = \ _ a - > a
- _ a = a
除此之外,您的代码相当合理,但可以通过使用函数 splitAt
splitAt :: Int - > [a] - > ([a],[a])
splitAt n xs =(取n xs,下降n xs)
changeChar x char xs =
let(before,_it:after )= splitAt(x - 1)
in ++(char:after)
这也突出了这个定义的一个小问题,那就是如果你的索引太大,它会引起模式匹配失败。我们可以通过让函数返回一个未修改的字符串来解决这个问题,如果我们结束了的话
changeChar x char xs = $在
[] - >之后的情况下,b $ b let(before,after)= splitAt(x-1)
[]
(_:rest) - > char:rest
这里有一个通用的模式,在一个特定的地方应用修改函数名单。这是我们如何提取的。
changeAt :: Int - > (a - > a) - > [a] - > [a]
changeAt n f xs =
在
[] - >之后的情况下,让(之前,之后)= splitAt(n-1)
[]
(x:rest) - > (fx):rest
我们可以用它来迭代这个概念
- |替换列表中处理为矩阵的元素。
changeMatrix ::(Int,Int) - > a - > [[a]] - > [[a]]
changeMatrix(i,j)x = changeAt i(changeAt j(const x))
I have been learning some Haskell and I came up with a solution to one of my exercise which I was trying to figure out .
- Changes a Char to another specified Char in a specified position in a String
changeStr :: Int -> Char -> String -> String
changeStr x char zs = (take (x-1) zs) ++ [(changeChar (head (take x zs)) char)] ++ (drop x zs)
- Changes a Char to another Char
changeChar :: Char -> Char -> Char
changeChar x y = y
I just wanted to ask is there any other way in which I could do this in a more simpler way using different methods ?
The thing that screams for generalization is changeChar
. It's actually very close to a very common Haskell Prelude
function called const
. To get changeChar
we just need to flip const
.
const :: a -> b -> a
const a b = a
changeChar :: Char -> Char -> Char
changeChar = flip const
-- = flip (\a _ -> a)
-- = \_ a -> a
-- _ a = a
Beyond that, your code is fairly reasonable, but can be cleaned up by using a function splitAt
splitAt :: Int -> [a] -> ([a], [a])
splitAt n xs = (take n xs, drop n xs)
changeChar x char xs =
let (before, _it:after) = splitAt (x - 1)
in before ++ (char:after)
which also highlights a slight problem with this definition in that if your index is too large it will throw a pattern matching failure. We could fix that by making the function return an unmodified string if we "fall off the end"
changeChar x char xs =
let (before, after) = splitAt (x - 1)
in case after of
[] -> []
(_:rest) -> char:rest
There's a general pattern here as well of applying a modifying function at a particular place in a list. Here's how we can extract that.
changeAt :: Int -> (a -> a) -> [a] -> [a]
changeAt n f xs =
let (before, after) = splitAt (n-1)
in case after of
[] -> []
(x:rest) -> (f x):rest
We can use this to iterate the notion
-- | Replaces an element in a list of lists treated as a matrix.
changeMatrix :: (Int, Int) -> a -> [[a]] -> [[a]]
changeMatrix (i, j) x = changeAt i (changeAt j (const x))
这篇关于Haskell - 在字符串中的指定位置将Char转换为另一个指定的Char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!