![]() |
| ||
| I need help with Scheme Hi! I am new to Scheme and I need help with this problem. Split and return a list of two lists at a given element; call it (split List Element). The given element should be the head of the second list. (split ‘(2 3 4 6) 4) => ((2 3) (6)). If the element is not in the list, a list consisting of the list and the null list are returned. This is what I have so far along with some other functions I had to do. Can someone please help me with this split function??? (define(split List Element) ;(c)splits and returns a list of two lists at a given element. The given element should be the head of the second list. If the element is not in the list, a list consisting of the list and the null list are returned. (cond ((equal?(member Element List)#F)(cons List ())) (else (cond ((null? List) (cons List ())) ((equal? Element (car List)) (cons () List)) )))) (define(remove Item List) ;(a)remove an element from a list and return the list without the element (cond ((null? List) List) ((equal? Item (car List)) (cdr List)) (else( cons (car List) (remove Item (cdr List)))))) (define(after Given List) ;(b)return the tail of a list after a given element (if element is not in list it returns the original list) (cond ((equal?(member Given List)#F)List) (else (cond ((null? List) List) ((equal? Given (car List))(cdr List)) (else( after Given(cdr List))))))) ;(d)returns the last element of a list. (define(last list) (cond ((null? (cdr list)) (car list)) (else (last (cdr list))))) ;(e)counts the elements (length) of a list. (define (count list) (cond((null? list) 0) (else (+ 1 (count (cdr list)))))) ;(f)Merges two (ascending) ordered lists of integers into a single (ascending)ordered list. (define (Merge a b) (cond ((null? a) b) ((null? b) a) ((< (car a) (car b)) (cons (car a) (Merge (cdr a) b))) ((>= (car a) (car b)) (cons (car b) (Merge a (cdr b)))) )) ;(g)Returns the shorter of two lists. (define (shorter l1 l2) (if (< (count l2) (count l1)) l2 l1)) |
| ||
| Re: I need help with Scheme Use code tags please. |
| ||
| Re: I need help with Scheme Quote:
Split and return a list of two lists at a given element; call it (split List Element). The given element should be the head of the second list. (split ‘(2 3 4 6) 4) => ((2 3) (6)). If the element is not in the list, a list consisting of the list and the null list are returned. This is what I have so far along with some other functions I had to do. Can someone please help me with this split function??? (define(split List Element) |
| ||
| Re: I need help with Scheme What the fuck. Do you realize how bad the indentation is? Aghh. |
| ||
| Re: I need help with Scheme Well, if you want to implement a split function, the first step would be for your code to handle all the possible cases. |
| ||
| Re: I need help with Scheme Look that is just how it copied over from Dr. Scheme...sorry you don't have to help me out ok |
| ||
| Re: I need help with Scheme The ironic thing is that your first pasting _was_ indented, if you look at when you quote it. And I do want to help, I just identified for you the main thing you need to do to implement split right now. What is stopping you from doing that? |
| ||
| Re: I need help with Scheme Quote:
(split '(2 3 4 6) 4)should return ((2 3) (4 6)), according to the specification. Quote:
(else (cond ;...clause in a condform is not sensible; the following does exactly the same, but clearer: (define (split List Element) Asking whether something is equal?to #fis better expressed as (false? something): (define (split List Element) What does memberdo? It walks the list, and as soon as it finds the element it is looking for, it returns the tail of the list including the element. If the element is not in the list, #f is returned. Well, this is almost what you need for the second part of your result, but how do you get the first part then? When you construct the first part, you will get the second almost automatically, so you don't need memberhere. You will need to walk the list somehow, most likely with a recursive helper function. Every element that is not the searched one gets pushed onto an intermediate list. As soon as you find the element, you reverse the intermediate list, and return it together with the tail of the original list. Feel free to show how far you got. |
| All times are GMT -4. The time now is 2:52 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC