functional FOR

- – functional FOR where: [a, b] – interval set, f – “FOR”-internal block:
for::Integer->Integer->(Integer->IO())->IO()
for a b f = foldr ((>>).f) (return()) [a..b]

- – printing prime numbers using functional FOR:
printRec2 a = do
for 1 a ch

- – printing prime numbers using recursion:
printRec a = do
if ( a > 1 )
then do
printRec (a-1)
ch a
else return()

- – procedure-printer:
ch a = do
if pr a
then putStr ( “The ” ++ show(a) ++ ” is a Prime number” ++ “\n” )
else return()

Full code:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

module Main where
import IO

- – function ost(a,b) returns residue of division of a number by b number
ost :: Integer -> Integer -> Integer
ost a b | a >= b = ost (a-b) b
ost a b | a < b = a

- – In mathematics, a prime number (or a prime) is a natural number
- – which has exactly two distinct natural number divisors: 1 and itself
pr :: Integer -> Bool
pr 0 = False
pr 1 = False
pr 2 = True
pr n = dne n 2 (n-1)

- – functional FOR: a, b – interval set, f – “FOR”-internal block
for::Integer->Integer->(Integer->IO())->IO()
for a b f = foldr ((>>).f) (return()) [a..b]

- – printing prime numbers using FOR
printRec2 a = do
for 1 a ch

- – printing prime numbers using recursion
printRec a = do
if ( a > 1 )
then do
printRec (a-1)
ch a
else return()

- – procedure-printer
ch a = do
if pr a
then putStr ( “The ” ++ show(a) ++ ” is a Prime number” ++ “\n” )
else return()

- – ned A B C == “There is no devisors of A at [B,C] segment”
dne :: Integer -> Integer -> Integer -> Bool
dne a b c | b == c = (ost a b /= 0)
dne a b c | b > c = True
dne a b c | b < c = (dne a b (c-1))&&(dne a c c)

readNum :: IO Integer
readNum = readLn

main = do
hSetBuffering stdout NoBuffering
putStr “Enter A: ”
a <- readNum
printRec a

Leave a comment

Your comment