Skip to content

Commit

Permalink
made == for LinkedLists iterative
Browse files Browse the repository at this point in the history
  • Loading branch information
arikheinss committed Oct 29, 2023
1 parent 4dd9022 commit 4e2a437
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ head(x::Cons) = x.head
tail(x::Cons) = x.tail

Base.:(==)(x::Nil, y::Nil) = true
Base.:(==)(x::Cons, y::Cons) = (x.head == y.head) && (x.tail == y.tail)
function Base.:(==)(x::LinkedList, y::LinkedList)
# can be changed to the more elegant
# Base.:(==)(x::Cons, y::Cons) = (x.head == y.head) && (x.tail == y.tail)
# once julia supports tail call recursions
while x isa Cons && y isa Cons
x.head == y.head || return false
x = x.tail
y = y.tail
end
if x isa Cons || y isa Cons
return false

Check warning on line 33 in src/list.jl

View check run for this annotation

Codecov / codecov/patch

src/list.jl#L33

Added line #L33 was not covered by tests
end
return true
end

function Base.show(io::IO, l::LinkedList{T}) where T
if isa(l,Nil)
Expand Down

0 comments on commit 4e2a437

Please sign in to comment.