Help with Scheme!!

Reply

Join Date: Feb 2009
Posts: 7
Reputation: jcoder is an unknown quantity at this point 
Solved Threads: 0
jcoder jcoder is offline Offline
Newbie Poster

Help with Scheme!!

 
0
  #1
Feb 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,003
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 137
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Help with Scheme!!

 
0
  #2
Feb 20th, 2009
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.

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: jcoder is an unknown quantity at this point 
Solved Threads: 0
jcoder jcoder is offline Offline
Newbie Poster

Re: Help with Scheme!!

 
0
  #3
Feb 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Help with Scheme!!

 
0
  #4
Mar 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 16
Reputation: bugista is an unknown quantity at this point 
Solved Threads: 0
bugista bugista is offline Offline
Newbie Poster

Re: Help with Scheme!!

 
0
  #5
Apr 20th, 2009
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.
Never Say Never
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 6
Reputation: Harleqin is an unknown quantity at this point 
Solved Threads: 1
Harleqin Harleqin is offline Offline
Newbie Poster

Re: Help with Scheme!!

 
0
  #6
Jun 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: lor101 is an unknown quantity at this point 
Solved Threads: 0
lor101 lor101 is offline Offline
Newbie Poster
 
0
  #7
32 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster
 
0
  #8
27 Days Ago
Yeah, just ask your questions and post some code on the forum. It's the better way, more people can answer your questions.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC