Declaratieve Talen/Oplossing haskell nqueens

Uit Wina Examenwiki
Versie door 10.4.198.161 (overleg) op 25 jun 2006 om 17:20
Naar navigatie springen Naar zoeken springen

Mogelijke oplossing

nqueens::Int->[Int]
-------------------
nqueens n = let
		list = [1..n]
		permlist = perms list
	     in
		getoklist permlist


getoklist::[[Int]]->[Int]
------------------------
getoklist [] = error "geen oplossing"
getoklist (x:xs) | listok x = x
		 | otherwise = getoklist xs


listok::[Int]->Bool
-------------------
listok [] = True
listok (a:as) = if (listok2 a as 1) == False then False
		else listok as
				
listok2::Int->[Int]->Int->Bool
------------------------------
listok2 _ [] _ = True
listok2 a (b:bs) kol | a == (b+kol) = False
		     | a == (b-kol) = False
		     | otherwise =  listok2 a bs (kol+1)
				 
					 
					 
--Permuteer de lijst
perms :: [k] -> [[k]]
---------------------
perms [] = [[]]
perms (x:xs) = concat (map (tussen x) (perms xs)) where 
            tussen e [] = [[e]]
            tussen e (y:ys) = (e:y:ys) : map (y:) (tussen e ys)