我想编写一个函数,返回一个数组,其所有子数组的长度必须为 2。例如返回将是[[1, 2], [3, 4]].
我定义:
(1) subset TArray of Array where { .all ~~ subset :: where [Int, Int] };
和
sub fcn(Int $n) of TArray is export(:fcn) {
[[1, 2], [3, 4]];
}
Run Code Online (Sandbox Code Playgroud)
我发现(1)过于复杂。有没有更简单的?
我试图将数字的平方分解为一个平方和:在Ruby中:
def decompose(n)
def decompose_aux(nb, rac)
return [] if (nb == 0)
i, l = rac, nil
while (i >= Math.sqrt(nb / 2.0).to_i + 1) do
diff = nb - i * i
rac = Math.sqrt(diff).to_i
l = decompose_aux(diff, rac);
if l != nil then
l << i; return l
end
i -= 1
end
l
end
l = decompose_aux(n * n, Math.sqrt(n * n - 1).to_i);
if l then return l else return nil end
end
Run Code Online (Sandbox Code Playgroud)
在Clojure中:
(defn decompAux …Run Code Online (Sandbox Code Playgroud) 我正在编写小函数来切断给定长度的数据包中的字符串,但我是Haskell的初学者,我想我可以简化我的功能.他们来了 :
packetAux _ [] = []
packetAux 0 ls = []
packetAux n l@(x:xs) = if n > (length l) then [] else x : packetAux (n - 1) xs
packet _ [] = []
packet 0 l = []
packet n l@(x:xs) = [x | x <- ((packetAux n l) : (packet n xs)), x /= ""]
Run Code Online (Sandbox Code Playgroud)
例如:包2"12345"给出["12","23","34","45"]
我怎么能避免1)packetAux和数据包中的重复2)用x/=""过滤数据包中的结果?
我有两个清单:
(def xxa ["olp" "xyz"])
(def xxb ["ulove" "alove" "holp" "sholp"])
Run Code Online (Sandbox Code Playgroud)
和一个函数试图获取第一个列表的元素作为第二个列表的元素的一部分:
(defn in-array
[array1 array2]
(for [s1 array1 :when (some #(.contains %1 s1) array2)] s1))
Run Code Online (Sandbox Code Playgroud)
(in-array xxa xxb)应返回["olp"]
但我得到:
IllegalArgumentException Key must be integer clojure.lang.APersistentVector.invoke
Run Code Online (Sandbox Code Playgroud)
我不明白这意味着什么.有人能给我一些启示吗?
我有一份清单清单:
[[5,1,0,0,5,5,0,0],[0,0,1,4,2,0,6,1],[1,1,6,3,0,1,0,0],[1,5,0,0,0,1,1,6]]
Run Code Online (Sandbox Code Playgroud)
和一个字符串"wxyz"
我想:1)
w: 5 1 0 0 5 5 0 0
x: 0 0 1 4 2 0 6 1
y: 1 1 6 3 0 1 0 0
z: 1 5 0 0 0 1 1 6
Run Code Online (Sandbox Code Playgroud)
我写:
f c xs = putStrLn (c : ':' : ' ' : concat (intersperse " " $ map show xs))
Run Code Online (Sandbox Code Playgroud)
写一行
和2)
g xxs c = mapM_ (f c) xxs
Run Code Online (Sandbox Code Playgroud)
如何修改2)循环字符串"wxyz"才能拥有1)?