【发布时间】:2021-06-02 17:09:23
【问题描述】:
我已经尝试对这个字符串列表进行排序十个小时了。这十个小时主要用于充实解决方法,但命运是残酷的,经过所有这些努力,我的解决方法仍然需要我对字符串列表进行排序而不考虑大小写。
我希望这个函数做的一个例子:
Input: ["In", "Thats", "pLAIN"]
Output: ["In", "pLAIN", "Thats"]
基本上我想对字符串进行排序,就好像它们都是小写一样,但保持输入的大小写
到目前为止我的代码:
--This function inserts the strings with capitals in them into the list of lower case only strings
--alphabetically
insertCapitals :: [String] -> [String] -> [String]
insertCapitals capitalList lowerList
--Base case
|capitalList == [] && lowerList == [] = []
--If the capital list is empty then append the rest of the lowerList
|capitalList == [] = (lowerListHead:insertCapitals capitalList tailedLowerList)
--If the lowerList is empty append the rest of the capitalList
|lowerList == [] = (capitalListHead:insertCapitals tailedCapitalList lowerList)
--If the current lowercase word is less than the current uppercase word then append the lowercase word
|compareResult == LT = (lowerListHead:insertCapitals capitalList tailedLowerList)
--If the current uppercase word is less than the current lowercase word then append the uppercase word
|compareResult == GT = (capitalListHead:insertCapitals tailedCapitalList lowerList)
此代码是我的变通方法尝试的一部分。它通过获取原始列表来工作,将其分解为完全小写字符串的列表和剩余的任何字符串的列表(所有这些都将具有大写字母)。我的代码的问题是它要求小写字符串列表和所有其他字符串列表都按字母顺序排列,而不考虑大小写。
基本上我的问题来了一个完整的循环。
提前感谢您的帮助
【问题讨论】:
标签: string list sorting haskell