Monday, May 30, 2011

Reverse a List

rev::[Int]->[Int]
rev (x:xs) = reverse (x:xs)

Factorial using recursion

fact::Int->Int
fact 0 =1
fact x | x >0 = x * fact (x-1)

Length of a list using length function

--Q4
lgn::[Int]->Int
lgn [] =0
lgn xs = length xs

nth Element of a List

kElem::[Int]->Int->Int
kElem (x:xs) n = xs !! n-x

Last Element -1 of a List

nElm::[Int]->Int
nElm [x]=x
nElm (x:xs) = reverse xs !! 1

Last Element of a List [Recursion ]

lstElm::[Int]->Int
lstElm [x]=x
lstElm (x:xs)=lstElm xs

Average of three integers

avg::Int->Int->Int->Int
avg a b c = (a+b+c) `div` 3

Average of a List

avg2::[Int]->Int
avg2 (x:xs) = (sum xs )`div` length xs