我怎么知道一个单词重复多少次?

时间:2016-06-06 17:45:44

标签: haskell

我有以下文字:"Hello world! I love this world"我需要知道" world"这个词有多少次?在文中重复。我怎么能用Haskell做到这一点?

2 个答案:

答案 0 :(得分:1)

for (int i=num; i>0; --i){
    //Insert spaces in order to center the diamond
    for (int n=0; n<i; ++n){
        System.out.print(" ");
    }
    System.out.print("*");
    for (int n=i; n<num; ++n){
        System.out.print("$*");
    }//Ending bracket of nested for loop
    System.out.println();
}//Ending bracket of for loop
//Print out a diamond shape based on user input
for (int i=0; i<=num; ++i){   //<= to print the last asterisk
    //Insert spaces in order to center the diamond
    for (int n=0; n<i; ++n){
        System.out.print(" ");
    }
    System.out.print("*");
    for (int n=i; n<num; ++n){
        System.out.print("$*");
    }//Ending bracket of nested for loop
    System.out.println();
}//Ending bracket of for loop

答案 1 :(得分:0)

愚蠢的方法是查看列表中的每个位置,并检查它是否是"world"的开头。计算你已经完成的职位。

Data.List> length . filter ("world" `isPrefixOf`) . tails $ "Hello world! I love this world"
2