Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme add element to the end of list [closed]

Tags:

scheme

racket

How can you add an element to the end of a list (before the empty) when only cons, first, rest, empty? and cond recursions can be used

like image 535
user1718737 Avatar asked Oct 04 '12 01:10

user1718737


People also ask

How do I get the last element in a list Scheme?

First find the lenght of a list by cdring it down. Then use list-ref x which gives the x element of the list. For example list-ref yourlistsname 0 gives the first element (basically car of the list.) And (list-ref yourlistsname (- length 1)) gives the last element of the list.

How do I use append in Scheme?

The append function joins two lists together to make one. The append function is built into Scheme. It concatenates two lists, that is to say, given two lists list1 and list2 it produces a new list which starts with the same elements as list1 and finishes with those of list2 .

Whats cons in Scheme?

The other constructor, cons , is used when you already have a list and you want to add one new element. Cons takes two arguments, an element and a list (in that order), and returns a new list whose car is the first argument and whose cdr is the second.

What does list do in Scheme?

Introducing Lists In contrast to Scheme's unstructured data types, such as symbols and numbers, lists are structures that contain other values as elements. A list is an ordered collection of values. In Scheme, lists can be heterogeneous, in that they may contain different kinds of values.


2 Answers

Think about how you would implement append (or, more generally, think about how you would implement a right-fold). Now, if you append a list to a singleton list containing your element-to-add, you've basically appended your element.

(Obviously, this is O(n), so don't add elements individually this way.)


Here's a solution using a right-fold:

(define (append-element lst elem)
  (foldr cons (list elem) lst))

and a solution using append:

(define (append-element lst elem)
  (append lst (list elem)))

So if you can implement either foldr or append yourself, using the operations you've listed (it's easy! try it), you're good to go.

P.S. In fact, you can implement append using a right-fold:

(define (append lst1 lst2)
  (foldr cons lst2 lst1))

but that still leaves you to implement foldr yourself. ;-) (Hint: it's easy. Look at my implementation of left-fold for starting ideas.)

like image 180
Chris Jester-Young Avatar answered Nov 10 '22 22:11

Chris Jester-Young


This looks like homework, so I'll give you some pointers to get you right on track, fill-in the blanks:

(define (add-last lst ele)
  (cond ((empty? lst)    ; if the list is empty
         <???>)          ; create a one-element list with `ele`
        (else            ; if the list is non-empty
         (cons <???>     ; cons the first element in the list
               <???>)))) ; with the result of advancing the recursion

The above can be implemented in terms of cons, first, rest, empty? and cond, no other procedures are needed.

like image 25
Óscar López Avatar answered Nov 10 '22 20:11

Óscar López