0% found this document useful (0 votes)
5 views

CPP 6

cpp exp 06

Uploaded by

atharvv1107
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CPP 6

cpp exp 06

Uploaded by

atharvv1107
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Name:Atharv Vede

ID:- VU4F2324073 SE IT-DIV A/BATCH D

EXPERIMENT 6
AIM:- Demonstrate recursive function in Haskell.

THEORY:- Recursive functions play a central role in Haskell, and are used throughout computer science
and mathematics generally. Recursion is basically a form of repetition, and we can understand it by
making distinct what it means for a function to be recursive, as compared to how it behaves. A recursive
function simply means this: function that has the ability to invoke itself. And it behaves such that it
invokes itself only when a condition complete, as with an if/else/then expression, or a pattern match
which contains at least one base case that generates the recursion, as well as a recursive case which
causes the function to call itself, creating a loop

SOURCE CODE FOR SUM OF n Number:

INPUT:- sum1 ::Int-> Int

sum1 n=ifn <2 then 1else n+sum1(n-1)

main ::10 ()

main=do

putStrLn "The sum of the first 10 numbers is:"

print (sum1 10).

OUTPUT:

GHCi, version 9.4.8:

:- The sum of the first 10 numbers is: 55

SOURCE CODE FOR FACTORIAL OF n Number:

INPUT: factorial : Int-> Int

factorial n=ifn < 0 then error "Factorial is not defined for negative numbers"

else if n == 0 then 7

else n * factorial (n-1)


main ::10 ()

main = do

putStrLn "The factorial of 10 is:'

print (factorial 10)

OUTPUT: GHCi, version 9.4.8:

:- The factorial of 10 is:

3628800

CONCLUSION:-Thus, We successfully implemented recursive function in Haskell

You might also like