Archived

This topic is now archived and is closed to further replies.

DrSCHEME HELP PLZ!!!

This topic is 5873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi can someone please help me with this DrScheme, I need to make a ton of functions in SCHEME, so far so good... but, I''ve gotten stuck on this one function can someone please help me: Make a function to Count the number of repeated occurrences from a given list list passed to Count (a b a a a c c) after applying Count it will yield ((1 a)(1 b)(3 a)(2 c)) THANKS!

Share this post


Link to post
Share on other sites
(define count-occurs-hlp
(lambda (l acc)
(cond [(null? l)
acc]
[(member2? (car l) acc)
(count-occurs-hlp (cdr l) (inc (car l) acc)]
[else
(count-occurs-hlp (cdr l) (cons (list (car l) ''1) acc)])))

(define member2?
(lambda (key l)
(cond [(null? l) #f]
[(equal? (car (car l)) key) #t]
[else (member2? key (cdr l))])))

(define inc
(lambda (key acc)
(cond [(null? l) acc]
[(equal? (car (car l)) key)
(cons (list key (second (car l)))
(cdr acc))]
[else (cons (car acc)
(inc key (cdr acc)))])))

(define second
(lambda (l)
(car (cdr l))))

Share this post


Link to post
Share on other sites
thanks so much for the help SanTec...
but i get errors when i try to run this...
(btw there were 2 parenthesis that i added because they were causing errors so heres the updated code)

(define count-occurs-hlp
(lambda (l acc)
(cond [(null? l)
acc]
[(member2? (car l) acc)
(count-occurs-hlp (cdr l) (inc (car l) acc))]
[else
(count-occurs-hlp (cdr l) (cons (list (car l) ''1) acc))])))

(define member2?
(lambda (key l)
(cond [(null? l) #f]
[(equal? (car (car l)) key) #t]
[else (member2? key (cdr l))])))

(define inc
(lambda (key acc)
(cond [(null? l) acc]
[(equal? (car (car l)) key)
(cons (list key (second (car l)))
(cdr acc))]
[else (cons (car acc)
(inc key (cdr acc)))])))

(define second
(lambda (l)
(car (cdr l))))

I execute the function with this command... (is that correct? sorry newbie in scheme)
> (count-occurs-hlp ''(a b c) ''(0))

but I get this error..
. car: expects argument of type ; given 0
>

please help if you can
thanks again

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Holy crap, keep it simple guys.

(define Count
(letrec ((F (lambda (cnt a lst)
(cond ((null? lst) (list cnt a))
((eq? a (car lst))
(F (+ cnt 1) a (cdr lst)))
(else (cons (list cnt a)
(F 1 (car lst) (cdr lst))))))))
(lambda (lst)
(if (null? lst)
''()
(F 1 (car lst) (cdr lst))))))

Share this post


Link to post
Share on other sites

(define (do-count lst var num res)
(if (null? lst)
(if (> num 0)
(append res (list (cons num var)))
res)
(if (equal? (car lst) var )
(do-count (cdr lst) var (+ 1 num) res)
(do-count (cdr lst) (car lst) 1 (append res (list (cons num var)))))))

(define (count lst)
(if (null? lst)
lst
(do-count lst (car lst) 0 '())))

(define list1 '(a b a a a c c))
(define list2 '(a a a b b b c c c a a b b c c a b c))


Edited by - Ranok on November 14, 2001 10:34:13 PM

Share this post


Link to post
Share on other sites
THANKS!!!!!! YOU GUYS RULE!!!
I got Ranok''s and also the anony poster''s codes to work!
thanks a lot guys!
now i have another challenge for ya

Expand the Count function to handle any level of nested list. List(a b a a a(a b c) c c (a b b c c c c)) after applying Count2 will yield:
((1 a)(1 b)(3 a)((1 a)(1 b)(1 c))(2 c)((2 c)((1 a)(2 b)(4 c)))

also i was just wondering u guys seem to know a lot about scheme is it worth the effort to master it?... I mean I''ve only started learning Scheme since last week and my COMP teacher had expected us to learn it in a week to do this project. Basically our project consists of 10 or so functions.. I did 8 of em already, u guys did the 9th , but i just need to do Count2 now! it would be great if u can help

THANKS AGAIN!!!!!!

Jason Arora
jason@pubism.com
http://www.pubism.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Scheme is a slick language. Pick up a copy of "The Little Schemer" and possible "The Seasoned Schemer" if you really want to master Scheme from a stylistic and elegance point of view. As for modifying Count to handle slsts, like I said, do your own homework.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by JasonA


also i was just wondering u guys seem to know a lot about scheme is it worth the effort to master it?...



LISP (Scheme is a type of LISP) Is widely used in Artifical Intelligence programming. If that has any interest for you, it''s worth learning scheme.


quote:

I mean I''ve only started learning Scheme since last week and my COMP teacher had expected us to learn it in a week to do this project. Basically our project consists of 10 or so functions.. I did 8 of em already, u guys did the 9th , but i just need to do Count2 now! it would be great if u can help




You need to try this on your own. If you are having any specific problems with your count function, ask questions on those. ''How do I do it?'' or ''Where do I start?'' are way too broad, and put us in the position of helping you cheat on your homework.

Share this post


Link to post
Share on other sites
Hi again,
sorry, I know.. I was getting greedy
sorry about that!
well it''s such an easy function to make.. is my logic correct:
look at element if it''s a list itself then call Count on it, and so on

i''m just not implementing it right.. (i don''t know enough about scheme) i''m trying to make the function test the overhead function for Count2.. so in other words:
(test ''(a b a a a(a b c) c c (a b b c c c c)))
but my returned list is completely wrong:
((1 a) (1 b) (1 a) (1 a) (1 a) (1 (a b c)) (1 c) (1 c) (1 (a b b c c c c)))

(btw Count2 is Anonymous Poster''s Count - thanks again)

(define test
(lambda (lst)
(cond
[(null? lst) ''()]
[else (cons (Count2 (list (car lst))) (test (cdr lst)))]
)
)
)
;a modified Count that can handle nested loops
(define Count2
;using local definitions, so the user doesn''t have to worry about count, element, or list1
(letrec
((F (lambda (count element list1)
(cond
;return a list constructed from count and element
[(null? list1) (list count element)]
;more dupes, increment count
[(equal? element (car list1)) (F (+ count 1) element (cdr list1))]
;found a nested element so perform Count2 on it
;[not (null? (cdr (list element))) (Count2 (list element))];(F 1 (car (list element)) (cdr (list element)))]
;no more duplicates, so finally return the list (count element)
[else (cons (list count element) (F 1 (car list1) (cdr list1)))]
)
)
)
)
(lambda (list1)
(if
(null? list1) ''() (F 1 (car list1) (cdr list1))
)
)
)
)

Share this post


Link to post
Share on other sites
You know, I''m going to close this. These are obvious homework questions, and pretty much any professor is going to consider this cheating.

Besides which, Scheme isn''t a particularly complicated language, nor are these problems all that difficult. Take the time to figure them out yourself or you''re going to have a hard time making it as a programmer.

Share this post


Link to post
Share on other sites

This topic is 5873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.