我正在研究一个更长的问题,我在列表表单中复制了N次元素,我相信使用append是正确的方法.这个小谓词理论上应该是这样的:
?- repl(x,5,L).
L = [x, x, x, x, x] ;
false.
Run Code Online (Sandbox Code Playgroud)
我似乎无法在网上找到任何提示,复制单个元素,但我相信我们需要使用append,但没有递归解决方案.我来自更多的Haskell背景,这个问题会更容易执行.有人可以帮我开始吗?:)
我的目前为止:
repl(E, N, R) :-
N > 0, append([E], [], R), writeln(R), repl(E, N-1, R), fail.
Run Code Online (Sandbox Code Playgroud)
这给了我:
?- repl(x,5,L).
[x]
[x]
[x]
[x]
[x]
false.
Run Code Online (Sandbox Code Playgroud)
关闭但不完全!
我是一个haskell新手,但我正在研究一个我称之为mfilter的函数,如果它们属于传入范围,它将从列表中排除元素,如下所示:
mfilter [(3,7)] [1..10] = [1,2,8,9,10]
mfilter [(10,18), (2,5), (20,20)] [1..25] = [1,6,7,8,9,19,21,22,23,24,25]
mfilter [('0','9')] "Sat Feb 8 20:34:50 2014" = "Sat Feb :: "
Run Code Online (Sandbox Code Playgroud)
在考虑范围时,我正在尝试编写一个从这些范围中排除数字的辅助函数,但是我遇到了很多打字问题,我不知道从哪里开始.这是我的代码:
mfilter :: Ord a => [(a, a)] -> [a] -> [a]
mfilter (range:t) list = mfilter t (map (exclude range) list)
exclude :: Ord a => (a, a) -> [a] -> [a]
exclude _ [] = []
exclude (first, last) (x:t)
| x < first && x > last = x : map (exclude …Run Code Online (Sandbox Code Playgroud) 我试图根据文件的最长公共前缀 cpfx 来匹配文件,并且对 haskell 有点陌生。我正在尝试获取列表列表并简单地返回它们共享的前缀。例如:
cpfx ["obscure","obscures","obscured","obscuring"] --> "obscur"
cpfx ["abc", "ab", "abcd"] --> "ab"
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用几个辅助方法,如下所示:
cpfx :: [[Char]] -> [Char]
cpfx [] = [] -- nothing, duh
cpfx (x:[]) = x -- only one thing to test, return it
cpfx (x:t) = cpfx' (x:t) 0 -- otherwise, need to test
cpfx' (x:[]) _ = []
cpfx' (x:t) n
-- call ifMatch to see if every list matches at that location, then check the next one
| ifMatch (x:t) n …Run Code Online (Sandbox Code Playgroud) 我试图获取整数列表并重复多次,但作为haskell中的字符串.预期的产出是:
> nnn [3,1,5] = ["3-3-3","1","5-5-5-5-5"]
> nnn [10,2,4] = ["10-10-10-10-10-10-10-10-10-10","2-2","4-4-4-4"]
Run Code Online (Sandbox Code Playgroud)
现在我觉得我很接近......但是我不能把一个int列表变成一个String,并且肯定不知道如何处理连字符.这是我到目前为止的代码,并输出:
nnn :: [Int] -> [[Char]]
nnn list = map show (map (\x -> take x $ repeat x) list)
Run Code Online (Sandbox Code Playgroud)
它给了我:
> nnn [3,1,5] = ["[3,3,3]","[1]","[5,5,5,5,5]"]
Run Code Online (Sandbox Code Playgroud)
(我至少接近!)有人能指出我正确的方向来看这里吗?:)