Author | Message |
---|---|
Amedrano
Posts: 80
|
Posted 17:49 Oct 19, 2015 |
(define (find element list)
(cond [(empty? list) list]
[(equal? element (first list)) list]
[else (find element (rest list))]))
in this block of code from the notes, on the 3rd line highlighted in blue, what does the last "list" do? what purpose does it serve? |
waaffles
Posts: 16
|
Posted 17:57 Oct 19, 2015 |
In that second condition, you're asking:
If the element passed in is equal to the first element of the list I'm passed in, return me that list in its entirety
|
darkserith
Posts: 45
|
Posted 17:57 Oct 19, 2015 |
the "list" in blue is the list that contains the element that you are looking for, plus the rest of the list. for example, if i have '(2 1 3), then if i want to look for 2 in '(2 1 3), then i call (find 2 '(2 1 3)). first element in '(2 1 3) is 2, so it says ok, i found it, so let me return '(2 1 3). if i wanted to find 1 in '(2 1 3) then the "list" in blue returns '(1 3). eat eat |
layla08
Posts: 70
|
Posted 17:58 Oct 19, 2015 |
(define (find element list) // Defining the function find. Passing in 2 parameters.
(cond [(empty? list) list] // If the list (the 2nd parameter) is empty, return itself
[(equal? element (first list)) list] // If the element = the first thing in the list, return list
[else (find element (rest list))])) // Else recursively call find, passing in element for the first parameter, and the rest of the list for the second parameter
|
Amedrano
Posts: 80
|
Posted 17:59 Oct 19, 2015 |
oooooOOOOOOoooo duh... thanks guys |