943,558 Members | Top Members by Rank

Ad:
Mar 7th, 2009
0

I need help with Scheme

Expand Post »
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))
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taylola is offline Offline
3 posts
since Mar 2009
Mar 7th, 2009
2

Re: I need help with Scheme

Use code tags please.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 7th, 2009
0

Re: I need help with Scheme

Use code tags please.
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))
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taylola is offline Offline
3 posts
since Mar 2009
Mar 7th, 2009
2

Re: I need help with Scheme

What the ****. Do you realize how bad the indentation is?

Aghh.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 7th, 2009
1

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.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 7th, 2009
0

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taylola is offline Offline
3 posts
since Mar 2009
Mar 7th, 2009
2

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?
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jun 2nd, 2009
0

Re: I need help with Scheme

Click to Expand / Collapse  Quote originally posted by taylola ...
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.
I believe that (split '(2 3 4 6) 4) should return ((2 3) (4 6)) , according to the specification.

Quote ...
(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))))))
OK, first a little cleanup: having an (else (cond ;... clause in a cond form is not sensible; the following does exactly the same, but clearer:

(define (split List Element)
  (cond ((equal? (member Element List) #F) (cons List ()))
        ((null? List) (cons List ()))
        ((equal? Element (car List)) (cons () List))))))

Asking whether something is equal? to #f is better expressed as (false? something) :

(define (split List Element)
  (cond ((false? (member Element List)) (cons List ()))
        ((null? List) (cons List ()))
        ((equal? Element (car List)) (cons () List))))))

What does member do? 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 member here.

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Harleqin is offline Offline
6 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: Inno setup creation
Next Thread in Legacy and Other Languages Forum Timeline: ANSI lisp compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC