Ok so I need to remove the last item of a list.
ie : (1 2 3 4)
I will get
(1 2 3)
my idea is as follows:
reverse(list)
car(list)
reverse(list)
however I don't know if there is simple function in scheme(racket) that does reverse?
If not, is there an easier way to approach this problem?
Thanks.
reversing a list
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: reversing a list
You idea is ok, but you need to translate it to scheme.
The function reverse does not change its argument, it instead creates a new list and returns it. That is, you have to write:
Note the parenthesis surrounding the name of the function and its argument, not only its argument.
About car, you got it wrong. The function car returns the first element, not the tail of the list. What you want is cdr. And you need to use setf again, because cdr does not change its argument, it just returns the correct value:
I think this is enough for you to get the idea. Good luck!
The function reverse does not change its argument, it instead creates a new list and returns it. That is, you have to write:
Code: Select all
(setf list (reverse list))
About car, you got it wrong. The function car returns the first element, not the tail of the list. What you want is cdr. And you need to use setf again, because cdr does not change its argument, it just returns the correct value:
Code: Select all
(setf list (cdr list))
-
- Posts: 11
- Joined: Wed Nov 02, 2011 11:56 pm
Re: reversing a list
Code: Select all
(define reverse-list
(lambda ( l )
(define lasts
(lambda ( l )
(cond
((null? (cdr l))(car l))
(else
(lasts (cdr l))))))
(define onleft
(lambda ( l )
(cond
((null? (cdr l))(quote ()))
(else
(cons
(car l)
(onleft (cdr l)))))))
(cond
((null? (cdr l))
(cons (car l)()))
(else
(cons (lasts l)
(reverse-list (onleft l)))))))
not the best but but it does work.
Re: reversing a list
Like most Schemes, Racket provides a 'reverse' procedure.
Your approach to reversing a list is basically sound, but you are performing some unnecessary steps (and it would be better if it handled the case of an empty list).
The traditional Scheme way of defining a function that loops through a list and returns some result (such as the reversed list) is to define an iteration function which accepts one additional argument, the result to be returned. Each iteration through the list, the result gets updated based upon the first item in the list and then the iteration repeats with the remainder of the list..
Your function will then call this iteration function with an appropriate initial value for the result (in your case, the empty list).
Of course, it makes sense to nest the definition of the iteration function within your function, to keep things organized and to avoid cluttering up your namespace.
And by having your reverse function handle an empty list (returning an empty list), your "last item removal" function becomes almost trivial. (And of course, you could just use the standard 'reverse' function instead.)
Your approach to reversing a list is basically sound, but you are performing some unnecessary steps (and it would be better if it handled the case of an empty list).
The traditional Scheme way of defining a function that loops through a list and returns some result (such as the reversed list) is to define an iteration function which accepts one additional argument, the result to be returned. Each iteration through the list, the result gets updated based upon the first item in the list and then the iteration repeats with the remainder of the list..
Code: Select all
(define (reverse-list-iter lis result)
(if (null? lis)
result
(reverse-list-iter (cdr lis) (cons (car lis) result)) ))
Code: Select all
(define (reverse-list lis)
(reverse-list-iter lis '()) )
Code: Select all
(define (reverse-list lis)
(define (iter lis result)
(if (null? lis)
result
(iter (cdr lis) (cons (car lis) result)) ))
(iter lis '()) )
Code: Select all
(define (butlast lis)
(if (null? lis)
'()
(reverse-list (cdr (reverse-list lis))) ))