0% found this document useful (0 votes)
104 views2 pages

I o Exercise

The document contains 4 programming exercises: 1) A program that prints a string a given number of times by passing the string and count as parameters. 2) A program that reads multiple lines of input and prints them in reverse order. 3) A program that reads a line of input, checks if it is a palindrome, and prints the result. 4) A program that reads integers from multiple lines until a 0 is entered, sums the numbers, and prints the total.

Uploaded by

tedmania
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views2 pages

I o Exercise

The document contains 4 programming exercises: 1) A program that prints a string a given number of times by passing the string and count as parameters. 2) A program that reads multiple lines of input and prints them in reverse order. 3) A program that reads a line of input, checks if it is a palindrome, and prints the result. 4) A program that reads integers from multiple lines until a 0 is entered, sums the numbers, and prints the total.

Uploaded by

tedmania
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

module Excercise1

where
{-
1) Write an I/O program to print something n times (the number of times to outpu
t the
string must be handled as a parameter of the program).
-}
printntime::(String,Int)->IO()
printntime (x,1) = putStr(x)
printntime (x,y) = do
putStr(x)
printntime (x,y-1)
{-
2) Write an I/O program that reads n lines, and then write them in the opposite
order
(reversed).
-}
pSlist::[String]->IO()
pSlist [] = putStrLn "--end"
pSlist (x:xs) = do
putStrLn x
pSlist xs
reverseLine::IO ()
reverseLine= do
putStrLn("enter datas.enter r to finish")
xs<-getLines
pSlist[y|y<-(reverse xs)]
getLines::IO [String]
getLines = do
x<-getLine
if x=="r" then
return ([""])
else
do
y<-getLines
return ((show x):y)
{-
3) Write an I10 program which will read a line of input and test whether the inp
ut is a
palindrome. The program should 'prompt' for its input and also output an appropr
iate
message after testing.
-}
palindrome::IO ()
palindrome = do
putStrLn("enter a worpad to test for palindrome")
x<-getLine
if (isPal(show x)) then
putStrLn("word is palindrome")
else
putStrLn("word is not a palindrome")
isPal::[Char]->Bool
isPal xs = if xs == reverse xs then True
else False
{-
4) Write a program to sum integers supplied one per line until zero is input. Th
e program
should prompt appropriately for its inputs and explain its output.
-}

summer::IO ()
summer = do
putStrLn("enter lines of number to add. finish with 0")
xs<-getInts
putStr("The sum is "++ show(sum xs))
getInts::IO [Int]
getInts = do
x<-getLine
if x=="0" then
return ([0])
else
do
y<-getInts
return ((read x):y)

You might also like