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.

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.

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.

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.

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.

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.

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.

Yeah, just ask your questions and post some code on the forum. It's the better way, more people can answer your questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.