-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.hs
More file actions
31 lines (25 loc) · 828 Bytes
/
linkedList.hs
File metadata and controls
31 lines (25 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--PREPEND TO A LIST (let y = Cons 1 Null) or (let x = Null)
data Node a = Cons a (Node a) | Null deriving Show
myHead :: Node a -> a
myHead Null = error "its empty"
myHead (Cons a _) = a
myLast :: Node a -> a
myLast Null = error "its empty"
myLast (Cons a Null) = a
myLast (Cons a b) = myTail b
addNode :: Node a -> Node a -> Node a
addNode Null y = error "you cant add a empty value"
addNode x Null = x
addNode x (Cons a Null) = Cons a x
addNode x (Cons a b) = Cons a(addNode x b)
removeNode :: Eq a => a -> Node a -> Node a
removeNode x Null = Null
removeNode x (Cons a (Null))
|a == x = Null
|otherwise = Cons a (Null)
removeNode x (Cons a b)
|a == x = b
|otherwise = Cons a (removeNode x b)
addNodeHead :: Eq a => a -> Node a -> Node a
addNodeHead x Null = Cons x Null
addNodeHead x y = Cons x (y)