在Haskell中,你如何乘以一个字符串列表? [英] In Haskell how can you multiply a list of string?

查看:91
本文介绍了在Haskell中,你如何乘以一个字符串列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个函数,该函数接受一个字符串和一个Int列表,并返回每个字符串都是int次的字符串列表。即:

I'm trying to write a function that takes a list of Strings and an Int and returns that list of strings in which every string is "int" times. That is:

duplicate :: [String] -> Int -> [String]
duplicate ["ab","ac","yt","hfg","lkj","poi"] 2 

输出应该是 $ b

the output should be

["ab","ab","ac","ac","yt","yt","hfg","hfg","lkj","lkj","poi","poi"]


推荐答案

duplicate = (. replicate) . (>>=)

或更多基本

or more basic

duplicate xs n = concatMap (replicate n) xs



<如果你想通过表示法进行列表乘法运算,那么你可以使用

if you want to have list multiplication by notation as well

> let (**) :: [a] -> Int -> [a]
|     (**) = (. replicate) . (>>=)

> ["a","b"]**3
["a","a","a","b","b","b"]

这篇关于在Haskell中,你如何乘以一个字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆