简体   繁体   English

Haskell 中字符串列表的长度

[英]length of list of strings in Haskell

I have no Idea how to complete my code and need help.我不知道如何完成我的代码并需要帮助。

The function I need should be able to calculate the number of characters in a list of strings.我需要的函数应该能够计算字符串列表中的字符数。

For example:例如:

charLena (createStrLst ["avd", "d4sf"] ["asvd","a2e","bdsh"])    --output is 7 (Total length of the first List)

charLenb (createStrLst ["avd", "d4sf"] ["asvd","a2e","bdsh"])    --output is 11 (Total length of the second List)

Those are the functions I have and can use:这些是我拥有并且可以使用的功能:

data StrLst = StrLst [String] [String] deriving (Eq)
StrLst :: [String] -> [String] -> StrLst 

createStrLst :: [String] -> [String] -> StrLst 
numa :: StrLst -> Int             --number of strings in a
numb :: StrLst -> Int             --number of strings in b
lena :: StrLst -> Int -> Int      --length of (i+1)-th string in a
lenb :: StrLst -> Int -> Int      --length of (i+1)-th string in b

createStrLst  a b = (StrLst a b)
numa (StrLst a b) = length a
numb (StrLst a b) = length b
lena (StrLst a b) i = length (a!!i)
lenb (StrLst a b) i = length (b!!i)

Now I need the length of the first List of Strings charLena and the second List of Strings charLenb .现在我需要第一个字符串列表charLena和第二个字符串列表charLenb

I am also allowed to use map and recursion and just basic commands of haskell (nothisng else)我也被允许使用map和 recursion 以及 haskell 的基本命令(nothisng else)

But how can I map through StrLst datatype??但是如何通过 StrLst 数据类型进行映射?

StrLst is not a list, so you can't use map directly. StrLst不是列表,因此不能直接使用map However, the two functions you are supposed to write appear responsible for deciding which list to map over:但是,您应该编写的两个函数似乎负责决定映射哪个列表:

charLena :: StrLst -> Int
charLena (StrLst a _) = ...

charLenb :: StrLst -> Int
charLenb (StrLst _ b) = ...

a and b are the first and second lists, respectively, stored in the StrLst value received as an argument. ab分别是存储在作为参数接收的StrLst值中的第一个和第二个列表。 Each has type [String] , and so is suitable for use as an argument to map .每个都有类型[String] ,因此适合用作map的参数。


Update: Since you don't have the ability to pattern match on the StrLst value, there is no way for you to get direct access to either list.更新:由于您无法对StrLst值进行模式匹配,因此您无法直接访问任一列表。 The only thing you can do is use lena and lenb to get lengths of individual strings from either list.您唯一能做的就是使用lenalenb从任一列表中获取单个字符串的长度。 In order to know what indices are valid arguments to lena / lenb , you need to use numa / numb to find the length of either list, then construct a range.为了知道哪些索引是lena / lenb有效参数,您需要使用numa / numb来查找任一列表的长度,然后构造一个范围。 For example, [0..numa s - 1] would give you a list of valid indices into the first list of s .例如, [0..numa s - 1]会给你一个有效索引列表到s的第一个列表中。

Once you have the index list, you can map the partially applied function lena s over it to give you all the individual string lists for list A, which you can then sum up to give you the return value of charLena .一旦你有了索引列表,你就可以将部分应用的函数lena s映射到它上面,为你提供列表 A 的所有单独的字符串列表,然后你可以总结起来为你提供charLena的返回值。

charLena s = let indices = [0..numa s - 1]
                 lengths = ... -- use map here
             in ...  -- add up the values in lengths here.

charLenb will be similar, but using numb and lenb instead. charLenb将类似,但使用numblenb代替。

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

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