hi there,
I always appreciate your help.
please check my code. The code is used for removing sub-list.
(defun remove-ex (list-value find-value)
(my-remove-helper list-value find-value nil ) )
(defun my-remove-helper (list-value find-value result )
(cond ((null list-value ) result )
((equal find-value (car list-value)) (my-remove-helper (cdr list-
value) find-value result )
)
((listp (car list-value)) (my-remove-helper (car list-value) find-
value (append result (list nil)))
)
(t ( my-remove-helper (cdr list-value) find-value (append result
(list (car list-value) ) ) ) )
)
)
my code operates like this ..
------------------------------------------------
[4] USER(236): (remove-ex '(A B C D E F A) 'A)
(B C D E F)
[4] USER(237): (remove-ex '((A B) C D E) '(A B))
(C D E)
[4] USER(238): (remove-ex '(((A B))) '(A B))
(NIL)
[4] USER(239): (remove-ex '((((A B)))) '(A B))
(NIL NIL)
------------------------------------------------
The fourth result is not what I expected. What I expected is ((NIL)).
I think the code below has to make somewhat of change.
------------------------------------------------
((listp (car list-value)) (my-remove-helper (car list-value) find-
value (append result (list nil))))
------------------------------------------------
but I can't find it. I'm very new to LISP and functional language..
Could you help me?? thanks~
On Mar 22, 11:47 pm, ········@gmail.com wrote:
> hi there,
>
> I always appreciate your help.
> please check my code. The code is used for removing sub-list.
>
> (defun remove-ex (list-value find-value)
> (my-remove-helper list-value find-value nil ) )
>
> (defun my-remove-helper (list-value find-value result )
> (cond ((null list-value ) result )
> ((equal find-value (car list-value)) (my-remove-helper (cdr list-
> value) find-value result )
> )
>
> ((listp (car list-value)) (my-remove-helper (car list-value) find-
> value (append result (list nil)))
> )
>
> (t ( my-remove-helper (cdr list-value) find-value (append result
> (list (car list-value) ) ) ) )
> )
> )
>
> my code operates like this ..
>
> ------------------------------------------------
> [4] USER(236): (remove-ex '(A B C D E F A) 'A)
> (B C D E F)
>
> [4] USER(237): (remove-ex '((A B) C D E) '(A B))
> (C D E)
>
> [4] USER(238): (remove-ex '(((A B))) '(A B))
> (NIL)
>
> [4] USER(239): (remove-ex '((((A B)))) '(A B))
> (NIL NIL)
> ------------------------------------------------
>
> The fourth result is not what I expected. What I expected is ((NIL)).
> I think the code below has to make somewhat of change.
>
> ------------------------------------------------
> ((listp (car list-value)) (my-remove-helper (car list-value) find-
> value (append result (list nil))))
> ------------------------------------------------
>
> but I can't find it. I'm very new to LISP and functional language..
> Could you help me?? thanks~
The recursion invariant isn't consistent. Assuming I've read your
intent, here's an easier way.
(defun remove-ex (tree item)
(cond ((atom tree) tree)
((equalp (car tree) item)
(remove-ex (cdr tree) item))
(t (cons (remove-ex (car tree) item)
(remove-ex (cdr tree) item)))))
From: Kent M Pitman
Subject: Re: please check my algorithm to remove sub-list.
Date:
Message-ID: <uiqzerqyz.fsf@nhplace.com>
········@gmail.com writes:
> I always appreciate your help.
> please check my code. The code is used for removing sub-list.
Is this a homework assignment?
People here are often willing to help with these things, but they need
to know if it's homework or not.
On Mar 22, 9:47 pm, ········@gmail.com wrote:
> hi there,
>
> I always appreciate your help.
> please check my code. The code is used for removing sub-list.
>
> (defun remove-ex (list-value find-value)
> (my-remove-helper list-value find-value nil ) )
>
> (defun my-remove-helper (list-value find-value result )
> (cond ((null list-value ) result )
> ((equal find-value (car list-value)) (my-remove-helper (cdr list-
> value) find-value result )
> )
>
> ((listp (car list-value)) (my-remove-helper (car list-value) find-
> value (append result (list nil)))
> )
>
> (t ( my-remove-helper (cdr list-value) find-value (append result
> (list (car list-value) ) ) ) )
> )
> )
>
> my code operates like this ..
>
> ------------------------------------------------
> [4] USER(236): (remove-ex '(A B C D E F A) 'A)
> (B C D E F)
>
> [4] USER(237): (remove-ex '((A B) C D E) '(A B))
> (C D E)
>
> [4] USER(238): (remove-ex '(((A B))) '(A B))
> (NIL)
>
> [4] USER(239): (remove-ex '((((A B)))) '(A B))
> (NIL NIL)
> ------------------------------------------------
>
> The fourth result is not what I expected. What I expected is ((NIL)).
> I think the code below has to make somewhat of change.
>
> ------------------------------------------------
> ((listp (car list-value)) (my-remove-helper (car list-value) find-
> value (append result (list nil))))
> ------------------------------------------------
>
> but I can't find it. I'm very new to LISP and functional language..
> Could you help me?? thanks~
Ruby:
def remove_ex list, find_value
result = []
list.each{ |x|
next if x == find_value
result <<
if x.class == Array
remove_ex( x, find_value )
else
x
end }
result
end