Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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_Haskell_String Length_Non Exhaustive Patterns - Fatal编程技术网

String 一种程序的实现,其中字符串的字符在haskell中重复一定的时间

String 一种程序的实现,其中字符串的字符在haskell中重复一定的时间,string,haskell,string-length,non-exhaustive-patterns,String,Haskell,String Length,Non Exhaustive Patterns,这是我家庭作业中的一个问题,因此,我非常感谢你的建议 这学期我正在学习Haskell,我的第一项作业要求我编写一个函数,输入2个字符串(string1和string2),并返回由第一个字符串string1的(重复)字符组成的字符串,直到创建了与string2长度相同的字符串 我只允许使用前奏功能length 例如:将asstring1视为“Key”,将我的名字视为string2,函数应返回“KeyKe” 以下是我到目前为止得到的信息: makeString :: Int -> [a

这是我家庭作业中的一个问题,因此,我非常感谢你的建议

这学期我正在学习Haskell,我的第一项作业要求我编写一个函数,输入2个字符串(
string1
string2
),并返回由第一个字符串
string1
的(重复)字符组成的字符串,直到创建了与
string2
长度相同的字符串

我只允许使用前奏功能
length

例如:将as
string1
视为“Key”
,将我的名字
视为
string2
,函数应返回
“KeyKe”

以下是我到目前为止得到的信息:

    makeString :: Int -> [a] -> [a]
    makeString val (x:xs)
        | val > 0 = x : makeString (val-1) xs
        | otherwise = x:xs
我不是直接给它两个字符串,而是给它一个整数值(因为我以后可以用它代替长度),但这给了我一个运行时错误:

*Main> makeString 8 "ahmed"

"ahmed*** Exception: FirstScript.hs: (21,1)-(23,21) : Non-exhaustive patterns in function makeString
我想这可能与我的列表用完并变成一个空列表有关(?)


非常感谢您的帮助。

我认为这段代码足以解决您的问题:

extend :: String -> String -> String
extend src dst = extend' src src (length dst)
    where
        extend' :: String -> String -> Int -> String
        extend' _ _ 0 = []
        extend' [] src size = extend' src src size
        extend' (x:xs) src size  = x : extend' xs src (size - 1)
extend'
函数将循环第一个字符串,直到它被使用,然后再次开始使用它

您还可以使用
take
cycle
类似的功能:

repeatString :: String -> String
repeatString x = x ++ repeatString x

firstN :: Int -> String -> String
firstN 0 _ = []
firstN n (x:xs) = x : firstN ( n - 1 ) xs

extend :: String -> String -> String
extend src dst = firstN (length dst) (repeatString src)
或者更通用的版本

repeatString :: [a] -> [a]
repeatString x = x ++ repeatString x

firstN :: (Num n, Eq n ) => n -> [a] -> [a]
firstN 0 _ = []
firstN n (x:xs) = x : firstN ( n - 1 ) xs

extend :: [a] -> [b] -> [a]
extend _ [] = error "Empty target"
extend [] _ = error "Empty source"
extend src dst = firstN (length dst) (repeatString src)
能够获取任何类型的列表:

>extend [1,2,3,4] "foo bar"
[1,2,3,4,1,2,3]

就像卡斯滕说的,你应该

  • 处理列表为空时的情况
  • 放下列表时,按下列表末尾的第一个元素
  • 当n为0或更低时返回空列表
例如:

makeString :: Int -> [a] -> [a]
makeString _ [] = []    -- makeString 10 "" should return ""
makeString n (x:xs)
    | n > 0 = x:makeString (n-1) (xs++[x])
    | otherwise = []    -- makeString 0 "key" should return ""
在ghci中尝试此功能:

>makeString (length "Ahmed") "Key"
"KeyKe"
注意:这个答案是用英文写的。将其另存为
Filename.lhs
,然后在GHCi中尝试

我认为在这种情况下,
length
是一个很好的例子。您可以单独使用递归和模式匹配来解决这个问题,甚至可以在很长的列表上使用。但首先要做的是

我们的功能应该是什么类型的?我们取了两个字符串,我们将一遍又一遍地重复第一个字符串,听起来像
string->string->string
。但是,这种“反复重复”并不是字符串所特有的:您可以对每种类型的列表都这样做,因此我们选择以下类型:

> repeatFirst :: [a] -> [b] -> [a]
> repeatFirst as bs = go as bs
好吧,到目前为止还没发生什么意外,对吧?我们根据
go
定义了
repeatFirst
,但仍然没有定义。在
go
中,我们希望将
bs
的项目与
的相应项目交换为
,因此我们已经知道了一个基本情况,即如果
bs
为空会发生什么:

>    where go  _     []     = []
如果
bs
不是空的怎么办?在这种情况下,我们希望使用
中的正确项作为
。因此,我们应该同时穿越这两个区域:

>          go (x:xs) (_:ys) = x : go xs ys
我们目前正在处理以下情况:空的第二个参数列表和非空列表。我们仍然需要处理空的第一个参数列表:

>          go []     ys     = 
在这种情况下会发生什么?嗯,我们需要从
as
重新开始。事实上,这是可行的:

>                              go as ys
这里的一切都在一个地方:

repeatFirst :: [a] -> [b] -> [a]
repeatFirst as bs = go as bs
   where go  _     []     = []
         go (x:xs) (_:ys) = x : go xs ys
         go []     ys     =     go as ys
请注意,如果没有约束条件,则可以使用
循环
zipWith
常量

repeatFirst :: [a] -> [b] -> [a]
repeatFirst = zipWith const . cycle

但这可能是另一个问题。

只是为了确保:您只能使用
长度
,并且必须在字符列表上使用递归和模式匹配来完成所有操作?代码中的错误很容易解释:当您的第二个参数(列表
[a]
)为空
[]
时,您无法将其与
(x:xs)
因此您必须为它包含一个案例(比如
makeString val[]=…
)在一个单独的直线中,因此您的
否则
大小写看起来很奇怪-如果
val
=0,为什么要返回完整的字符串-您不想在那里使用空字符串吗?作为一个附加提示:您看到,由于从
(x:xs)中删除
x
,字符已经用完了
-也许您想将它们保存到某个地方供以后使用(这可能是在什么地方…提示:您想以某种方式将它们交给对
makeString
的递归调用供以后使用;))是的,我只允许使用“length”,并且必须使用递归和模式匹配来完成所有操作。至于你的第二个答复,我需要单独处理这样一个案件吗?如果我可以再次加载我的第一个列表并重复这样做,是否可能?编辑:我明白你想说什么了。谢谢!:)您可以删除
extend'
的第二个参数。谢谢您的帮助!这是一个很好的指针,但我不允许使用除“length”以外的任何其他函数@Zeta我可以,但更明确的是那里发生了什么。@AhmedZaidi您没有使用任何其他函数。第一个是解决你的问题的函数,我很确定你可以使用Haskell语法