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) |
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
Re mykth.hs
The function should return k-th element of the list. What is the type of elements of that list? (Hint: look at the type of mykth's first argument.)
Did you make an honest attempt to understand compiler error message? Sometimes that's all you have.
Please update the function to fail with meaningful error message if
k
is out of boundaries.P.S.:
[1, 2, 3, 4, 5]
can be more easily expressed as[1..5]
.