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 mykth.hs
mykth :: [a] -> Integer -> Integer
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.)
[1 of 1] Compiling Main ( /tmp/YYY.hs, interpreted )
/tmp/YYY.hs:2:30: error:
• Couldn't match expected type ‘Integer’ with actual type ‘a’
‘a’ is a rigid type variable bound by
the type signature for:
mykth :: forall a. [a] -> Integer -> Integer
at /tmp/YYY.hs:1:1-34
• In the expression: x
In an equation for ‘mykth’:
mykth (x : xs) k
| k == 1 = x
| otherwise = mykth xs (k - 1)
• Relevant bindings include
xs :: [a] (bound at /tmp/YYY.hs:2:10)
x :: a (bound at /tmp/YYY.hs:2:8)
mykth :: [a] -> Integer -> Integer (bound at /tmp/YYY.hs:2:1)
|
2 | mykth (x:xs) k | k == 1 = x
| ^
Failed, 0 modules loaded.
Did you make an honest attempt to understand compiler error message? Sometimes that's all you have.
λ mykth [1..5] 0
*** Exception: /tmp/YYY.hs:(2,1)-(3,45): Non-exhaustive patterns in function mykth
λ mykth [1..5] 6
*** Exception: /tmp/YYY.hs:(2,1)-(3,45): Non-exhaustive patterns in function mykth
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]
.
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 listsum.hs
It should be
listsum :: Num a => [a] -> a
, because(+)
restricts the typea
to instances of the classNum
(see Type constraints).Also it is unorthodox to put a blank line between type signature and definition. Please remove such blank lines (line 3 in listsum.hs).