943,733 Members | Top Members by Rank

Ad:
Feb 18th, 2009
0

Help with Scheme!!

Expand Post »
Hello all, I'm going for my masters and for the first time ever I am having a hard time with a programming language. This scheme is incredibly hard for me for some reason. The assignment is as follows:

I have a list, make a function that returns false only if the parameter is a list which contains a subsequence of 2 atoms or 2 lists in a row.

So I need a function that goes through a list and returns true if the members alternate between list and atom.

I spent hours trying to use a cond statement, now I'm trying with Ifs. So I know I will need recursion obviously. Here is what I am thinking:

1.Make sure the paramter is a list.
2. If the car is an atom, then check the ctr, if this is an atom, return false.
3.If the car is a list(else)? I want to check the ctr, if it's a list then return false, if it's not a list then call the function again.

I just can't figure this out, I have read online and my notes and I have stared at it for hours trying to come up with something.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jcoder is offline Offline
7 posts
since Feb 2009
Feb 20th, 2009
1

Re: Help with Scheme!!

What is the ctr? What are you talking about.

You should regard this as a problem of approaching the list from one of three cases: the previous element having been a list, having been an atom, or none of the above. If you face the problem by writing three separate functions for each of these situations, you can easily and readably solve this problem recursively.

Quote ...
This scheme is incredibly hard for me for some reason.
The reason is that you suck at programming, a fact stated objectively because you're having trouble with this assignment. The good news is that this course might correct some of that.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Feb 20th, 2009
0

Re: Help with Scheme!!

Wow, that was harsh.... every single programming course I took in my 4 year degree from c++ to java and VB with some php I got A+ in. I also have a couple of apps in for Google's Android platform and I program with Java for a living with the company I work for. I don't think I suck at programming, rather I just think this is giving me a very hard time because the syntax is very different from what I have been tought in the past. I never said I'm the best programmer cause I know I can be a lot better but still, that wasn't a very welcoming comment to what seems to be a forum for beginners.

I meant cdr. it was a typo and I put ctr. Anyway, I ended up doing 2 if statements, with one of the else clauses calling the function recursively. It works great except at the end after I pass it an empty list I am not catching the () at the right place and it causes the program to crash.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jcoder is offline Offline
7 posts
since Feb 2009
Mar 25th, 2009
0

Re: Help with Scheme!!

I think that this is what you need:

(require mzlib/compat)

(define alternate?
  (lambda (ls)
    (letrec ([alter-helper (lambda (pos1 pos2 ls)
                             (if (< pos2 (length ls))
                                 (let ([list-at-pos1? (list? (list-ref ls pos1))][list-at-pos2? (list? (list-ref ls pos2))]
                                       [atom-at-pos1? (atom? (list-ref ls pos1))]
                                       [atom-at-pos2? (atom? (list-ref ls pos2))])
                                   (if (or (and list-at-pos1? list-at-pos2?)(and atom-at-pos1? atom-at-pos2?))
                                       #f
                                       (alter-helper (add1 pos1)(add1 pos2) ls)
                                       ))
                                   #t))])
      (cond
        ((null? ls) #t)
        ((= (length ls) 1) #t)
        (else (alter-helper 0 1 ls))
        )
      )
    )
  )

(alternate? (list '(3 4) '(1 2)))  ;#f
(alternate? (list 5 '(1 2)))  ;#t

I love writing Scheme (it's a special language), so I wrote this with a lot of fun. Hope this helps, even if the thread is old.
Last edited by GDICommander; Mar 25th, 2009 at 10:44 pm.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Apr 20th, 2009
0

Re: Help with Scheme!!

I love writing Scheme (it's a special language), so I wrote this with a lot of fun. Hope this helps, even if the thread is old.

One of the most interesting and easiest language is Scheme. I learnt Scheme in just one month and wrote, one of the toughest program in my project.
Last edited by bugista; Apr 20th, 2009 at 2:56 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bugista is offline Offline
19 posts
since Apr 2009
Jun 2nd, 2009
0

Re: Help with Scheme!!

I would do this with two mutually recursive functions:

(define (list-next? ls)
  (cond ((null? ls) #t)
        ((list? (car ls))
         (atom-next? (cdr ls)))
        (#t #f)))

(define (atom-next? ls)
  (cond ((null? ls) #t)
        ((atom? (car ls))
         (list-next? (cdr ls)))
        (#t #f)))

(define (alternating? ls)
  (cond ((null? ls) #t)
        ((list? (car ls))
         (atom-next? (cdr ls)))
        (#t (list-next? (cdr ls)))))

I am unsure if Scheme has the atom? function built in, you might need to define it as

(define (atom? thing)
  (not (list? thing)))

Note that being a pair and being a list are two different things: not all pairs are also lists. Lists are pair chains that are terminated by the empty list. If you actually meant pairs, not lists, you will need to replace "list?" with "pair?" in the above.

"Structure and Interpretation of Computer Programs" is a nice, free book to learn Scheme, and a lot of Computer Science along the way.

Disclaimer: all code here is untested, no warranty.

Finally, please don't let the parentheses dangle around.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Harleqin is offline Offline
6 posts
since Jun 2009
Oct 9th, 2009
0
Re: Help with Scheme!!
hi. i was wondering if u could help with a project (seein that u like scheme). i have an idea what's goin on but i think i need another perspective. i'm not stuck per say, just having problems moving on because i keep having to go back and correct things that were already working.
is it possible for me to email the assignment to you and want i've done so far.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lor101 is offline Offline
1 posts
since Oct 2009
Oct 13th, 2009
0
Re: Help with Scheme!!
Yeah, just ask your questions and post some code on the forum. It's the better way, more people can answer your questions.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008

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: Computer Networks question
Next Thread in Legacy and Other Languages Forum Timeline: please help me write this prog in lisp





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


Follow us on Twitter


© 2011 DaniWeb® LLC