Learning Haskell: How to Implement My Own Version of the init Function

As part of Haskell training, I am trying to implement my own version of various list related functions. Right now I'm stuck in the init function . The init function in Haskell returns all the items in the list other than the last item.

Here is what I have done so far.

init' :: [Int] -> [Int]
init' [] = error "This function cannot be applied to an empty list"
init' [x] = [x]
init' (x:xs) = x : init' xs
+3
source share
4 answers

Your problem is your base case. Here:

init' [x] = [x]

, , . . , (, , ).

init' [x] = []

, ,

init' :: [a] -> [a]

'a', -, . , init ' . , init ' "abcde" "abcd"

+12

:

init' [x] = []

, , - .

+4
init' [x] = [x]

. , , .

+3

.

init' :: [a] -> [a]

init' []  = error("This is not right, empty list is not allowed here")
init' (x:[]) = [x]
init' (x:xs:[]) = [x]
init' (x:xs) = x: init' xs
0

Source: https://habr.com/ru/post/1732450/


All Articles