How to implement ++ in Haskell?

user3928256

Hi I'm new in Haskell programming. I'm trying to implement operator "++" by myself. Here's a small program I wrote but it won't work:

append (es:e) xs = 
    if (null es)
    then e:xs
    else append es (e:xs)

I received lots of type error with [a],[[a]] and [[[a]]]. Still confusing about the list type in Haskell. Can someone help me with that? Thank you. :)

Bergi
append (es:e) xs =
        ^^^^

Typically, you would write (e:es), which could be spoken "one e before a list of es". You actually have used this meaning below, but have told the compiler that es is an element and e is a list - which produces the type errors you received.

    if (null es)

That's not how you should test for an emtpy list. In fact, if you'd call append [] … you'd get a "non-exhaustive pattern" error, because (e:es) is always a list of at least one element. So try two patterns:

append []     xs = xs
append (e:es) xs = append es (e:xs)

However, this still doesn't work expected - the first list is actually reversed by this snippet. Instead, the first element (e) needs to go before the whole rest of the list (es and xs):

append (e:es) xs = e : (append es xs)

(and that's indeed how ++ is implemented)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to implement mathematics induction on Haskell

From Dev

How to implement Haskell equivalent of invokeAll

From Dev

How to implement BST searching in Haskell

From Dev

How to implement delete with foldr in Haskell

From Dev

Beginner Haskell - how to implement this function?

From Dev

Beginner Haskell - how to implement this function?

From Dev

How to implement Haskell's splitEvery in Swift?

From Dev

How to implement "symmetric non-strict or" in Haskell

From Dev

How to implement a fast, lazy KDTree in Haskell?

From Dev

How do I implement nested functions in haskell

From Dev

How to implement Church encoding division in haskell?

From Dev

How to implement custom ordering for data types in Haskell?

From Dev

How to implement search in file system in haskell?

From Dev

How to implement the equivalent of Go's select statement for Haskell STM channels?

From Dev

How can I implement a collection with O(1) indexing and mutability in Haskell?

From Dev

What is the difference between 1:[ ] and [1]? How would you implement : and [ ] in Haskell?

From Dev

How do I implement a partially injective type family in Haskell?

From Dev

How to implement a Read instance with the actual parsing already complete, in Haskell?

From Dev

Haskell: How to implement a processing on a list of different but related types?

From Dev

Implement insert in haskell with foldr

From Dev

Haskell Implement an IO code

From Dev

implement polynomial multiplication in Haskell

From Dev

Trying to implement binary search in haskell

From Dev

Haskell MonadState implement put with modify

From Dev

Haskell MonadState implement put with modify

From Dev

Implement recursion using foldl in haskell

From Dev

How could this code enter an infinite loop? aka how to implement a counter in Haskell?

From Dev

How can I implement a for loop in Haskell that iterates through all values of a list?

From Dev

How to implement f (g x) (h x) point-freely in Haskell?

Related Related

  1. 1

    How to implement mathematics induction on Haskell

  2. 2

    How to implement Haskell equivalent of invokeAll

  3. 3

    How to implement BST searching in Haskell

  4. 4

    How to implement delete with foldr in Haskell

  5. 5

    Beginner Haskell - how to implement this function?

  6. 6

    Beginner Haskell - how to implement this function?

  7. 7

    How to implement Haskell's splitEvery in Swift?

  8. 8

    How to implement "symmetric non-strict or" in Haskell

  9. 9

    How to implement a fast, lazy KDTree in Haskell?

  10. 10

    How do I implement nested functions in haskell

  11. 11

    How to implement Church encoding division in haskell?

  12. 12

    How to implement custom ordering for data types in Haskell?

  13. 13

    How to implement search in file system in haskell?

  14. 14

    How to implement the equivalent of Go's select statement for Haskell STM channels?

  15. 15

    How can I implement a collection with O(1) indexing and mutability in Haskell?

  16. 16

    What is the difference between 1:[ ] and [1]? How would you implement : and [ ] in Haskell?

  17. 17

    How do I implement a partially injective type family in Haskell?

  18. 18

    How to implement a Read instance with the actual parsing already complete, in Haskell?

  19. 19

    Haskell: How to implement a processing on a list of different but related types?

  20. 20

    Implement insert in haskell with foldr

  21. 21

    Haskell Implement an IO code

  22. 22

    implement polynomial multiplication in Haskell

  23. 23

    Trying to implement binary search in haskell

  24. 24

    Haskell MonadState implement put with modify

  25. 25

    Haskell MonadState implement put with modify

  26. 26

    Implement recursion using foldl in haskell

  27. 27

    How could this code enter an infinite loop? aka how to implement a counter in Haskell?

  28. 28

    How can I implement a for loop in Haskell that iterates through all values of a list?

  29. 29

    How to implement f (g x) (h x) point-freely in Haskell?

HotTag

Archive