From 4e2a4378b9c5051bc742e545f0820417f72e15d1 Mon Sep 17 00:00:00 2001 From: arikheinss Date: Sun, 29 Oct 2023 14:07:52 +0100 Subject: [PATCH] made == for LinkedLists iterative --- src/list.jl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/list.jl b/src/list.jl index 9189b0d9..05f23879 100644 --- a/src/list.jl +++ b/src/list.jl @@ -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 + end + return true +end function Base.show(io::IO, l::LinkedList{T}) where T if isa(l,Nil)