Jump to content

Balanced brackets: Difference between revisions

m
→‎{{header|Haskell}}: (Applied hlint, hindent)
No edit summary
m (→‎{{header|Haskell}}: (Applied hlint, hindent))
Line 3,114:
isBalanced :: String -> Maybe Int
isBalanced = bal (-1) 0
where
where bal :: Int -> Int -> String -> Maybe Int
bal _ 0 :: Int -> Int -> []String -> =Maybe NothingInt
bal i _ 0 [] = Just iNothing
bal i (-1) _ [] = Just i
bal i n ('[':bs-1) _ = balJust (i+1) (n+1) bs
bal i n ('][':bs) = bal (i + 1) (n- + 1) bs
bal i n (']':bs) = bal (i + 1) (n - 1) bs
 
-- Print a string, indicating whether it contains balanced brackets. If not,
Line 3,125 ⟶ 3,126:
check :: String -> IO ()
check s = maybe (good s) (bad s) (isBalanced s)
where
where good s = printf "Good \"%s\"\n" s
bad s ngood = printf "Bad Good \"%s\"\n%*s^\n" s (n+6) " "
where goodbad s n = printf "GoodBad \"%s\"\n%*s^\n" s (n + 6) " "
 
main :: IO ()
Line 3,132 ⟶ 3,134:
let bs = cycle "[]"
rs <- replicateM 10 newStdGen
zipWithM_ (\n r -> check $ shuffle (take n bs) r) [0,2 ..] rs</lang>
</lang>
We put our list shuffling function in a separate module. For efficiency we use ''mutable'' vectors, although for the short lists in our example it doesn't really matter.
<lang haskell>module VShuffle
( shuffle
module VShuffle (shuffle) where
) where
 
import Data.List (mapAccumL)
Line 3,145 ⟶ 3,147:
 
-- Generate a list of array index pairs, each corresponding to a swap.
pairs
pairs :: (Enum a, Random a, RandomGen g) => a -> a -> g -> [(a, a)]
pairs l u r = snd $ mapAccumL step r [l..pred u]
=> a -> a -> g -> [(a, a)]
where step r i = let (j, r') = randomR (i, u) r in (r', (i, j))
pairs l u r = snd $ mapAccumL step r [l .. pred u]
where
step r i =
where step r i = let (j, r') = randomR (i, u) r in (r', (i, j))
in (r', (i, j))
 
-- Return a random permutation of the list. We use the algorithm described in
-- http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm.
shuffle
shuffle :: (RandomGen g) => [a] -> g -> [a]
:: (RandomGen g)
shuffle xs r = V.toList . runST $ do
shuffle :: (RandomGen g) => [a] -> g -> [a]
v <- V.unsafeThaw $ V.fromList xs
shuffle xs r =
mapM_ (uncurry $ M.swap v) $ pairs 0 (M.length v - 1) r
shuffle xs r = V.toList . runST $ do
V.unsafeFreeze v
do v <- V.unsafeThaw $ V.fromList xs
</lang>
mapM_ (uncurry $ M.swap v) $ pairs 0 (M.length v - 1) r
V.unsafeFreeze v</lang>
Here's some sample output.
<pre>
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.