[Conflict resolve nordland@csee.ltu.se**20081111141619] { hunk ./examples/MasterMind_descr.html 1 -
- - - - --examples> ./MasterMind -Welcome to Mastermind! -Choose your secret. Press return when ready. - -My guess: Red Blue Blue Red -Answer (two integers): 1 1 -My guess: Blue Black Yellow Red -Answer (two integers): 2 0 -My guess: Green Black Red Red -Answer (two integers): 0 0 -My guess: Blue Blue Yellow White -Answer (two integers): 3 0 -My guess: Blue Blue Yellow Blue -Answer (two integers): 3 0 -My guess: Blue Blue Yellow Yellow -Answer (two integers): 4 0 -Yippee! -Do you want to play again? (y/n) n -examples> --The program is too long to display here. We only note a few things: -
-As a reactive program, this is quite degenerate: when started, it -expects -a positive integer n>2 as command line argument. It -computes all primes smaller than or equal to n, prints -the number of such primes to stdout and terminates. -
-The algorithm used illustrates imperative programming using updatable -arrays in Timber. Here is the program: -
-module Primes where - -import POSIX - -root env = class - limit :: Int - limit = parse (env.argv!1) - primesBound = limit `div` log3 limit - - primes := uniarray primesBound 0 - count := 0 - - isPrime k = loop 0 - where loop n = do - p = primes!n - if p*p > k then - result True - elsif k `mod` p == 0 then - result False - else loop (n+1) - - checkFrom k = do - p <- isPrime k - if p then - primes!count := k - count := count + 1 - if k < limit then checkFrom (k+1) - - result action - primes!0 := 2 - count := 1 - checkFrom 3 - env.stdout.write (show count++"\n") - env.exit 0 - - -log3 :: Int -> Int -log3 n - | n < 3 = 0 - | otherwise = 1 + log3 (n `div` 3) - --
-The root action notes that 2 is a prime and initiates count -to 1; it then checks numbers for primeness, starting with 3 in -procedure checkFrom. The iterative check is expressed using -recursion, checking for successive numbers up to limit. -
-The procedure isPrime works by trial division, using already -discovered primes: to check whether k is a prime, one tries -to find a proper prime factor p such that p*p <= k. If -none is found, k is prime. - - - rmfile ./examples/Primes_descr.html hunk ./examples/menu.html 1 - -
- - - - -