Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在Haskell中以列表表示法打印字符串_String_List_Haskell_Character_Notation - Fatal编程技术网

String 在Haskell中以列表表示法打印字符串

String 在Haskell中以列表表示法打印字符串,string,list,haskell,character,notation,String,List,Haskell,Character,Notation,我有一个函数,其中字符串按特定次数重复打印。但是,如果给定负数,函数应该打印出一个空列表 main = print (repeat (-1) "Hello World") repeat 1 x = x repeat n x | n < 0 = *???* | n > 0 = x ++ repeat (n-1) x main=print(重复(-1)“你好世界”) 重复1x=x 重复nx |n0=x++重复(n-1)x 我必须做些什么,以便将空列表打印在方括号中,而

我有一个函数,其中字符串按特定次数重复打印。但是,如果给定负数,函数应该打印出一个空列表

main = print (repeat (-1) "Hello World")

repeat 1 x = x

repeat n x

  | n < 0 = *???*

  | n > 0 = x ++ repeat (n-1) x
main=print(重复(-1)“你好世界”)
重复1x=x
重复nx
|n<0=**
|n>0=x++重复(n-1)x

我必须做些什么,以便将空列表打印在方括号中,而不是引号中?

让我们看看
repeat

repeat :: Int -> [a] -> [a]

我们希望
n有一个“空”的例子,不清楚你到底想做什么,因此问题出在哪里,所以这里有一些可能的解释

  • 您打算像当前一样构建一个字符串,但希望将空字符串打印为“[]”而不是“%”
  • 作为一个例子,请考虑: 让test=if(True)然后[]else['a'] 打印测试 “”

    如果这是问题所在,我不知道如何更改默认行为。最好的方法是避免使用“打印”,而是结合使用“putStrLn”和您自己的打印功能:

    myShow :: String -> String
    myShow "" = "[]"
    myShow a = a
    
    main = putStrLn . myShow $ (repeat (-1) "Hello World")
    
    repeat 1 x = [x]
    repeat n x 
      | n < 0 = []
      | n > 0 = x : repeat (n-1) x
    
  • 实际上,您打算构建一个字符串列表,默认情况下为空字符串:
  • 在这种情况下,您需要更改repeat函数:

    myShow :: String -> String
    myShow "" = "[]"
    myShow a = a
    
    main = putStrLn . myShow $ (repeat (-1) "Hello World")
    
    repeat 1 x = [x]
    repeat n x 
      | n < 0 = []
      | n > 0 = x : repeat (n-1) x
    
    重复1x=[x]
    重复nx
    |n<0=[]
    |n>0=x:重复(n-1)x
    

    现在签名将是
    重复::(Num a,Ord a)=>a->t->[t]
    ,空的大小写将是[]。

    您的问题实际上是使用Haskell的“无限”列表可以获得一个优雅的解决方案。其思想是生成一个输入字符串的无限重复列表,然后根据需要精确获取,然后决定如何打印该有限子列表:

    printNTimes :: Int -> String -> IO ()
    printNTimes n s = putStrLn . customShow . take n . repeat $ s
      where
        customShow [] = "[]"
        customShow ss = concat ss
    
    标准的
    Prelude
    函数,
    repeat
    (与您自己定义的函数不同),只需“永远”重复一次,就可以获得某个事物的单个示例,并创建该事物的无限列表。因此:


    当然,我们永远无法在一台毕竟只有有限内存的机器上显示或以任何其他完全具体的方式实现无限列表。但是这不是问题,因为我们只需要列表的第一个
    n
    元素,这就是
    takenn
    在上面的函数中所做的。请注意,当
    n仍为
    重复0“某些字符串”
    打印引号时!
    printNTimes :: Int -> String -> IO ()
    printNTimes n s = putStrLn . customShow . take n . repeat $ s
      where
        customShow [] = "[]"
        customShow ss = concat ss
    
    repeat "hello" = ["hello", "hello", "hello", ...]   -- not valid Haskell
    repeat 3 = [3, 3, 3, ...]   -- not valid Haskell
    
    showNTimes :: Int -> String -> String
    showNTimes n = customShow . take n . repeat
      where
        customShow [] = "[]"
        customShow ss = concat ss
    
    main :: IO ()
    main = do
        s <- getLine
        ...
        putStrLn $ showNTimes 10 s
        ...