ListA ([String]) ve örnek konumlar ListB ([Int]) öğelerini içeren iki liste var liste anlamalarını kullanarak yeni bir ListC ([String]) nasıl oluşturulur?Haskell List Anlaşılır listeA listB -> listC
sol numarası (ListB bakınız) daima
Step 1: get elem 1, add the head of the ListC
ListC = ["a"]
Step 2: get elem 2, add the head of the ListC
ListC = ["c","a"]
Step 3: get elem 1, add the head of the ListC
ListC = ["b","c","a"]
so the full chain:
a b c -> 1 2 1 -> a -> c a -> b c a
daha şablonları daha haklı:: Örneğin
ListA::[String]
ListB::[int]
ListC::[String]
ListA ListB ListC
a b c -> 3 2 1 -> a b c
a b c -> 2 2 1 -> a c b
a b c -> 3 1 1 -> b a c
a b c -> 1 2 1 -> b c a
a b c -> 2 1 1 -> c a b
a b c -> 1 1 1 -> c b a
bu fonksiyon (her not geçerli bir sayısal dizileri üretmektir sol eleman, bir öncekinden daha fazla, en az 1, yani baş en büyük elementtir)
module Main (main) where
import System.Random
main :: IO()
randomList :: Int -> [Int] -> StdGen -> [Int]
randomList 0 xlist _ = reverse xlist
randomList n xlist gen = randomList (n-1) (randomVal : xlist) gen'
where (randomVal, gen') = randomR (1,n) gen
shuffle :: [Int] -> [String] -> [String] -> [String]
shuffle [] _ deckB = deckB
shuffle pl deckA deckB = shuffle (tail pl) (hs ++ tail ts) (head ts : deckB)
where (hs, ts) = splitAt (pos-1) deckA
pos = head pl
ranks = ["2","3","4","5","6","7","8","9","T","J","Q","K","A"]
suits = ["C","D","H","S"]
deck = [rank ++ suit | suit <- suits, rank <- ranks]
main = do
gen <- newStdGen
let len = 52 :: Int
let permutationList = randomList len [] gen
let newDeck = shuffle permutationList deck []
print permutationList
print deck
print "-------------------------------------"
print newDeck
3 parçadan oluşan bir kart destesi tuttuğunuzu hayal edin, rastgele bir kart alıp masanın üzerine atın, sonra diğerine ve sonuncusu, şimdi tablonun bir araya getirilmiş kart listesi var, Yeni bir güverte kafası son kart atılır. https://en.wikipedia.org/wiki/Fisher-Yates_shuffle –
Şimdiye kadar neler denediniz? Bu bana ev ödevi isteği gibi biraz okuyor. –
kodun tamamı için güncellenmiş, bir poker sunucusu yazacağım opensource –