HVect
'un tüm alt boyutlarını HVect
olarak belirlemek istiyorum.HVect'in tüm alt boyutları nasıl belirlenir?
Örnek:
import Data.HVect
myHVect : HVect [Int, String, List Nat]
myHVect = [42, "text", [1, 2, 3]]
subDimensions : HVect [ HVect [Int], HVect [Int, String], HVect [Int, String, List Nat] ]
subDimensions = subDimHVect myHVect
-- [ [42], [42, "text"], [42, "text", [1, 2, 3]] ]
Benim yaklaşım şöyle görünür:
subDimHVect v = subDimHVect' [] [] v
where
subDimHVect' result _ [] = result
subDimHVect' result lastDim (x::xs) =
let nextDim = lastDim ++ [x] in
subDimHVect' (result ++ [nextDim]) nextDim xs
ama doğru subDimHVect
ve subDimHVect'
yazmayı bilmiyorum. Uygulama güzel gibi gözüküyor: Örnek
Manuel Hesaplama:
subDimHVect [42, "text", [1, 2, 3]]
= subDimHVect' [] [] [42, "text", [1, 2, 3]]
= subDimHVect' [[42]] [42] ["text", [1, 2, 3]]
= subDimHVect' [[42], [42, "text"]] [42, "text"] [[1, 2, 3]]
= subDimHVect' [[42], [42, "text"], [42, "text", [1, 2, 3]]] [42, "text", [1, 2, 3]] []
= [[42], [42, "text"], [42, "text", [1, 2, 3]]]
Ben İdris ve bağımlı türleri için oldukça yeni. Eksik tip imzaları bulmak için biraz yardım isterim.
Düzenleme: Ben de (definition of reverse
) türü çözemedim rağmen yazmak daha kolay olabilir başka bir yaklaşım bulundu: Örnek
subDimHVect v = reverse (subDimHVect' (reverse v))
where
subDimHVect' [] = []
subDimHVect' (x::xs) = [(x::xs)] ++ (subDimHVect' xs)
Manuel Hesaplama (kullanarak ikinci yaklaşım):
subDimHVect [42, "text", [1, 2, 3]]
= reverse (subDimHVect' (reverse [42, "text", [1, 2, 3]]))
= reverse (subDimHVect' [[1, 2, 3], "text", 42])
= reverse ([ [[1, 2, 3], "text", 42] ] ++ (subDimHVect' ["text", 42]))
= reverse ([ [[1, 2, 3], "text", 42] ] ++ [ ["text", 42] ] ++ (subDimHVect' [42]))
= reverse ([ [[1, 2, 3], "text", 42] ] ++ [ ["text", 42] ] ++ [ [42] ] ++ (subDimHVect' []))
= reverse ([ [[1, 2, 3], "text", 42] ] ++ [ ["text", 42] ] ++ [ [42] ] ++ [])
= reverse ([ [[1, 2, 3], "text", 42], ["text", 42], [42] ])
= [ [42], [42, "text"], [42, "text", [1, 2, 3]] ]
Teşekkürler, ama 'subDimensions' içinde böyle bir değişken loş 'yok. – maiermic
@MegaMuetzenMike Üzgünüm, bu diziyi karıştırın. Şimdi doğru olmalı! – xash