如何让我的字符串重复x次? [英] How to get my string repeated x times?

查看:152
本文介绍了如何让我的字符串重复x次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Haskell的完全noob,我无法让我的代码工作,我不知道如何解决它!我需要帮助:)如果有人有一个想法,我需要看看为了解决我的问题,我会非常有益的想法和推动正确的方向。



我试图创建一个C#string.Format类型,直到列表完成。这个列表是由userinput创建的,然后我只想要一个字符串被重复直到列表完成。

  test ::整数 - > String 
let list_n [0..k]
test k = putStrLn(r * r)| r< - list_n - 我的想法是,我强制
- 将整个列表放到r上,并且只要
--list中有更多的重复列表,但我甚至不会确定这是可能的:任何人有更好的想法如何做到这一点?我想所有的结果是一行而不是一行,因此我尝试创建ittereration,但是在HaskeLL中,说起来容易,然后完成:/ $ / $>

解决方案

这里有两个建议:一个试图匹配你发布的代码,另一个试图匹配你发布的英语。这似乎主要是一个关于语法的问题,所以我不确定有很多有意义的解释可以去

   - 匹配代码
test :: Int - > ; String
test k = concat [show(r * r)| r < - [0..k]]

- 匹配英文
test :: Int - >字符串 - >字符串
test ks = concat [s | r< - [0..k]]


I'm a complete noob to Haskell I cant get my code working at all and i have no clue on how to fix it! I need help :) If someone has an idea of where i need to look in order to fix my issue i would be extremly greatful for ideas and nudges in the right direction.

I am trying to create a type of C# string.Format that repeats until a list is finished. The list is created by the userinput and then i just want a string to be repeated untill the list is finished.

    test :: Integer -> String
    let list_n [0..k]
    test k = putStrLn (r * r) | r <- list_n   --My idea here is that i am forcing 
    --the entire list onto r and making it repeated as long as there is more in the
    --list, But im not even sure that is possible :(

Anyone has a better idea on how to do this? I want all the results in a line and not a row therefore im trying to create ittereration but in HaskeLL that is easier said then done :/

解决方案

Here are two proposals; one tries to match the code you posted, and the other tries to match the English you posted. This seems to be mostly a question about syntax, so I'm not sure there's a lot of meaningful explanation that can go along with this other than "read a tutorial".

-- match the code
test :: Int -> String
test k = concat [show (r * r) | r <- [0..k]]

-- match the English
test :: Int -> String -> String
test k s = concat [s | r <- [0..k]]

这篇关于如何让我的字符串重复x次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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