Created
August 24, 2017 05:54
-
-
Save rajanikantchirmade/b985dca03aa3fed573ea41219aff22a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listsum :: [a] -> a | |
listsum [] = 0 | |
listsum (x:xs) = x + listsum xs | |
main = do | |
let l = [1,2,3,4,5] | |
print $ listsum l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.