2014-10-18 59 views
2

我正在爲從k值選擇中繪製的n個元素重複排列編寫代碼。所以我的結果集的基數應該有k^n個元素。在Haskell中,這相當簡單。例如,我們可以這樣寫:將Haskell代碼轉換爲標準ML(與重複組合)

進口Control.Monad(replicateM)

主要= mapM_打印(replicateM 2 [1,2,3])

那麼你會得到列表如下:

[1,1] [1,2] [1,3] [2,1] [2,2] [2,3] [3,1] [3,2] [3 ,3]

但在標準ML上,我不知道該怎麼做。

我嘗試這樣做:

樂趣combs_with_rep(K,XXS)=

case (k, xxs) of (0,_) => [[]] 
        |(_, []) => [] 

        |(k, x::xs) =>List.map (fn ys => x::ys) (combs_with_rep((k-1),xxs))@ combs_with_rep(k,xs) 

不過這個名單是不完整的,我不知道爲什麼....

Haskell中有沒有模擬編碼做同樣的事情?或者我應該如何解決我的sml代碼?

任何幫助表示讚賞!

回答

1

剛剛改造一元代碼:

rep_comb n xs -- n times choose 1 elem from xs, repetition allowed 
    = replicateM n xs 
    = sequence $ replicate n xs 
    = foldr k (return []) $ replicate n xs 
     where 
      k m m' = do { x <- m; xs <- m'; return (x:xs) } 
    = case n of 0 -> [[]] ; 
       _ -> k xs (rep_comb (n-1) xs) 
     where 
      k m m' = m >>= (\x-> 
        m' >>= (\xs -> return (x:xs))) 
    = case n of 0 -> [[]] ; 
       _ -> xs >>= (\y-> 
        rep_comb (n-1) xs >>= (\ys -> [y:ys])) 
    -- i.e. 
    = case n of 0 -> [[]] ; 
       _ -> [y:ys | y<- xs, ys<- rep_comb (n-1) xs] 
    = case n of 0 -> [[]] ; 
       _ -> concatMap (\y-> map (y:) (rep_comb (n-1) xs)) xs 
    -- or, in a different order 
    = case n of 0 -> [[]] ; 
       _ -> [y:ys | ys<- rep_comb (n-1) xs, y<- xs] 
    = case n of 0 -> [[]] ; 
       _ -> concatMap (\ys-> map (:ys) xs) (rep_comb (n-1) xs) 

現在你可以翻譯這ML。