简体   繁体   English

用条件在Haskell中缩短字符串

[英]Shortening a string in Haskell with a condition

Chomp will take the longest amount of repeated characters from a string and one will limit this to 9, eg if given the string "aaaaabbbcc" , the answer would be "aaaaa" Chomp将从字符串中获取最长的重复字符,并且将其限制为9,例如,如果给定字符串“ aaaaabbbcc”,答案将为“ aaaaa”

I need to define a function, runs, which will do a similiar thing but it will put the string into separate lists, eg if the given string is "aaaaabbbccc" , the answer would be ["aaaaa","bbb","cc"] , and I need to use the munch function to do this. 我需要定义一个函数,运行,它会做类似的事情,但是会将字符串放入单独的列表中,例如,如果给定的字符串是"aaaaabbbccc" ,答案将是["aaaaa","bbb","cc"] ,并且我需要使用munch函数来执行此操作。

The condition of 9 characters applies too, so if the given string is "aaaaaaaaaa" , the answer would be ["aaaaaaaaa","a"] 9个字符的条件也适用,因此如果给定的字符串为"aaaaaaaaaa" ,则答案为["aaaaaaaaa","a"]

I've not actually got any implementation apart from something that I found which does pretty much does the same thing without the limit of 9 characters: 除了9个字符的限制外,我发现实际上可以做相同的事情的是,我实际上没有任何实现:

runs :: String -> String
runs x = group x

I thought of 2 ways of doing this, but I have no clue on the actual implementation, with one being to run the munch function for however many unique characters there are, ie if there is an x amount of a , b , c in the given string, it would run 3 times, and then put those lists together into one list. 我想到了两种方法,但是我对实际的实现一无所知,其中一种方法是针对任何唯一字符运行munch函数,即如果x中的a,b,c数量相等给定字符串,它将运行3次,然后将这些列表放到一个列表中。

Another way that I thought of is to use guards. 我想到的另一种方法是使用警卫。 If the number of any single character in the given string is 9 or less, then just use the group function, otherwise, shorten it down using munch, or something like that. 如果给定字符串中任何单个字符的数量为9个或更少,则只需使用group函数,否则,使用munch或类似的方法将其缩短。

Is anyone able to tell me if the two ideas I mentioned would work at all or suggest an alternative and how to get started? 有谁能告诉我我提到的两个想法是否可以奏效或提出替代方案以及如何开始? I'm a bit lost. 我有点迷路了。

here is another approach 这是另一种方法

define a split function to break list at fixed size chunks 定义一个拆分函数以固定大小的块中断列表

splitn :: Int -> [a] -> [[a]]
splitn _ [] = []
splitn n x = take n x : (splitn n $ drop n x)

now you can write your function as 现在您可以将函数编写为

runs =  concatMap (splitn 9) . group

A quick google gives you exactly what you're looking for. 快速的Google会为您提供所需的确切信息。

https://codereview.stackexchange.com/questions/158183/string-splitting-function-in-haskell https://codereview.stackexchange.com/questions/158183/string-splitting-function-in-haskell

If it works consider upvoting their answer as I only copied a link 如果可行,请考虑提高他们的答案,因为我只复制了一个链接

The basic strategy here is to take each unique list element to identify successive list elements that are identical. 这里的基本策略是采用每个唯一列表元素来标识相同的连续列表元素。 This let you have list elements in any mixed order. 这使您可以按任何混合顺序使用列表元素。 There are three functions but no imports. 有三个功能,但没有导入。

The first function is rd which creates the list of unique elements. 第一个函数是rd ,它创建唯一元素的列表。

The second function, t9 is because there might be over 18 of the same elements. 第二个功能t9是因为可能有18个以上相同的元素。 t9 will create 9 character long list segments of identical elements with the remainder as the last list (string). t9将创建9个字符的长列表段,这些段由相同的元素组成,其余部分作为最后一个列表(字符串)。

The final, unnamed function uses rd to compile lists of all elements matching each unique elements. 最后的未命名函数使用rd来编译与每个唯一元素匹配的所有元素的列表。 It uses t9 to create segments of 9 elements. 它使用t9创建9个元素的线段。

l = "bbbbbbbbbaaaaaaaaaaaadddadaaaaaaacccccccccc"
rd [] = []; rd (x:xs) | elem x xs = rd xs | otherwise = x:rd xs
t9 [] = []; t9 xs = [take 9 xs] ++ t9 (drop 9 xs)
[ t | f <- rd l, s <- [[ g | g <- l, f == g ]], t <- t9 s ]

["bbbbbbbbb","dddd","aaaaaaaaa","aaaaaaaaa","aa","ccccccccc","c"] [ “bbbbbbbbb”, “DDDD”, “AAAAAAAAA”, “AAAAAAAAA”, “AA”, “ccccccccc”, “C”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM