Skip to content

Instantly share code, notes, and snippets.

@rajanikantchirmade
Created August 24, 2017 05:54
Show Gist options
  • Save rajanikantchirmade/b985dca03aa3fed573ea41219aff22a3 to your computer and use it in GitHub Desktop.
Save rajanikantchirmade/b985dca03aa3fed573ea41219aff22a3 to your computer and use it in GitHub Desktop.
listsum :: [a] -> a
listsum [] = 0
listsum (x:xs) = x + listsum xs
main = do
let l = [1,2,3,4,5]
print $ listsum l
mykth :: [a] -> Integer -> Integer
mykth (x:xs) k | k == 1 = x
| otherwise = mykth xs (k - 1)
main = do
let l = [1, 2, 3, 4, 5]
let k = 3
print $ mykth l k
mylast :: [a] -> a
mylast [] = error "Empty list"
mylast [x] = x
mylast (_:xa) = mylast xa
main = do
let l = [1, 2, 3, 4]
let cl = "rajanikant"
print $ mylast l
print $ mylast cl
secondlast :: [a] -> a
secondlast[] = error "Empty list."
secondlast(x:xs) = if length xs == 1 then x
else secondlast xs
main = do
let l = [1, 2, 3, 4, 5]
print $ secondlast (l)
@vvv
Copy link

vvv commented Aug 24, 2017

Re secondlast.hs
Lines 2, 4: No blank line please.
Lines 3, 5: Put a space between function name and its argument.
Line 10: No brackets are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment