954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with Scheme

Hello all,

I had to write a program that would take a list and check for alternate atoms and list, if there were two atoms in a row, it would #f, same for two lists. I now need to make it #f for 3 in a row. This is what I did for the first ass.

#lang scheme
(define a (list 1 (list 2 3) 1  (list 2 3) 4)) ;one list
(define b (list (list 2 3) (list 2 3) 4)) ;one list
(define c (list 1 (list 2 3) 1 (list 2 3) 4)) ;one list
(define d (list 1 1 (list 2 3) 1 (list 2 3) 4)) ;one list
(define e (list 1)) ;one list


(define (altlis lis1)         
          
(if (list? (car lis1)) 
    (if (list? (car(cdr lis1))) 
        (quote(false)) 
        (altlis (cdr lis1)
                )
        ) 
    (if (null? (cdr lis1)) (quote(true))
        (if (list? (car(cdr lis1))) 
            (altlis (cdr lis1)) 
            (quote(false)))
        )
    )     
  
)

(altlis e)


I cannot figure this out to catch the third one, any help will be appreciate it as I have NO experience with scheme prior to this class I am taking.

Thanks

jcoder
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

First, make your life easier by making a function

(define (name x)
  (cond ((pair? x) 'pair)
        ((symbol? x) 'symbol)
        (else (error "i don't know how to handle this case"))))


Once you have this, make a recursive function that takes three arguments: a symbol (whose value is 'pair or 'symbol), an integer saying how many times that kind of thing occurred before the start of the current list, and a list of stuff.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Rashakil,

Thank you. I did think about that even for the first program, however for some reason I think he only wants a list passed as a parameter. Your suggestion takes a lot of the complication away so I will try to implement and discuss with the professor.

jcoder
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I have modified the code above to what I need, since everything is a list essentially I will always pass just the car of whatever is next - It seems to be working with a couple of examples, now I will work on the main recursive function:

(define (name x)
  (cond ((not(pair? x)) 'atom)
        ((list? x) 'list)
        (else (error "i don't know how to handle this case"))))
jcoder
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Wanted to let you know that it looks like I completed it. Thank you!

jcoder
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

It's a good thing, because I'm banned and wasn't going to give any further help.

Rashakil
Newbie Poster
Banned
9 posts since Jan 2006
Reputation Points: 51
Solved Threads: 0
 

Wow, why? you were great help... Anyway.. i read his requirements, he wanted one parameter function... So I did another function passed it the list and made that call my function with 3 paramaters lol :)

jcoder
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Oh, it was because Ken Hess made some racist comments on his blog and Rashakil (me) called him out on it.

harzipan
Newbie Poster
Banned
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You