Scheme list manipulation error - scheme

(define (create-polygon ptlist)
(if (null? ptlist) '()
(cons (cons (car ptlist) (cadr ptlist)) (create-polygon (cdr ptlist)))))
(define (newlist ptlist)
(append ptlist (car ptlist)))
(define test-points
(list p1 p2 p3 p4 p5 p6))
(create-polygon test-points)
I'm trying to make a list of a polygons edge's coordinates. p1,p2 are some points like (0 0).
The final result should be ((p1 p2) (p2 p3) (p3 p4) (p4 p5) (p5 p6) (p6 p1)).
I've wasted so much time on this to try and figure out what is giving me an error. And why it is giving an error.What am I doing wrong here?

This was some fun finger exercise on my lunch break.
Petite Chez Scheme Version 8.4
Copyright (c) 1985-2011 Cadence Research Systems
> (define test-points '(p1 p2 p3 p4 p5 p6))
> (define (rotate xs) (append (cdr xs) (list (car xs))))
> (define (create-polygon ptlist)
(map list ptlist (rotate ptlist)))
> (create-polygon test-points)
((p1 p2) (p2 p3) (p3 p4) (p4 p5) (p5 p6) (p6 p1))
My rotate function does what I think you were trying to do with your newlist function. In create-polygon I used the map higher-order function to do what you did with recursion.

Related

DFA in Scheme (homework)

Working on a homework question asking to write a DFA-acceptor in Scheme. Alphabet: {0, 1} Start-state: {Q0} Final-state: {Q2}. String must have a 01 in the sequence to be accepted.
States:
Q0 on 1 transitions to Q1.
Q0 on 0 transitions to Q0.
Q1 on 1 transitions to Q2.
Q1 on 0 transitions to P.
Q2 on 0 and 1 transitions to Q1.
The teacher requested each state to be a function and to return a function that it transitions to. (Q0 1) returns Q1, etc. Code below is an attempt to get things running, before worrying about checking if 01 is in the string. Currently getting an error: "Q0: unbound identifier;" and am not sure why after doing some searching. Any pointers would be helpful. The question is for homework, so not looking for direct answers. Thank you!
#lang racket
(define DFA-trans '((Q0 x) (Q1 x) '((Q2 x)) (P x)))
(define x '(1 1 0 1 0))
(define P(null? 1))
(define Q2 (lambda(x)
(if (null? (car x))
#t
(if (equal? (car x) 0)
(Q2 (cdr x))
(if (equal? (car x) 1)
(Q2 (cdr x))
#t
)))))
(define Q1 (lambda(x)
(if (null? (car x))
#f
(if (equal? (car x) 0)
(P (cdr x))
(if (equal? (car x) 1)
(Q2 (cdr x))
#f
)))))
(define Q0 (lambda(x)
(if (null? (car x))
#f
(if (equal? (car x) 0)
(Q0 (cdr x))
(if (equal? (car x) 1)
(Q1 (cdr x))
#f
)))))
(define DFA(map eval DFA-trans))
(DFA)
Don't use EVAL for this.
The definitions of P, Q0, Q1, Q2 look good.
If we can define DFA directly, you can just delete DFA-trans.
Assuming Q0 is the start state, I think you can simply do this
(define DFA (lambda () (Q0 x)))
And consider using COND instead of nested IF.

Recursively adding to a list

The problem I have is that I have a list full of coordinates about a polygons corners called p1, p2, p3, p4. I need to recursively add them to a new list like this ((p1,p2) (p2,p3) (p3,p4) (p4,p1). And I have this list of them to start of (p1 p2 p3 p4). How do I recursively add them to a new list?
I am not expert in Racket, so take this solution with grain of salt. You can append the first value of list to end, and compute pairs by recursively.
#lang racket
(define (pair-list l)
(match l
[(cons a (cons b '())) (cons (cons a b) '())]
[(cons a (cons b t)) (cons (cons a b) (pair-list (cons b t)))]))
(define (compute-values l)
(pair-list (append l (cons (first l) '()))))
(compute-values (list 1 2 3 4))
'((1 . 2) (2 . 3) (3 . 4) (4 . 1))

Scheme and let errors

I can't for the life of me find out why this code produces the error.
Here's the error:
let*: bad syntax (missing body) in: (let* ((tempp2 (p2) (letrec ((mloop (p1 p2) (if (= (length p1) 0)) (else if ((= (length p2) 0) ((set! p2 (pn)) (multiloop (cdr p1) (p2)))) (else (let* ((prod (append (prod) (cons (* (coeff (car p1)) (coeff (car p2))) (+ (expon (car p1)) (expon (car p2)))))))) (set! p2 (cdr p2)) (mloop (p1 p2)) (simplify (sort newone))))))))))
Here's the code:
(define multiplyPoly (lambda (p1 p2)
(
(let* ((hardp2 (p2)
(letrec
((mloop (p1 p2)
(if (= (length p1) 0))
(else if ((=(length p2) 0) ((set! p2 (hardp2)) (multiloop (cdr p1) (p2))))
(else
(let* ([prod (append (prod) (cons(*(coeff(car p1)) (coeff(car p2))) (+(expon(car p1)) (expon(car p2)))))]))
(set! p2 (cdr p2))
(mloop (p1 p2))
(simplify (sort newone)))))))))))))
You have lots of syntax errors in your code. I urge you to install a IDE that does matching of parentheses and ident your code properly, like DrRacket. DrRacket supports both R5RS and R6RS so you can still program in Scheme with DrRacket.
Usually a let, let* and letrec has the following form:
(let ((var expression) ...)
body)
In a letrec or letrec* the expression looks like (lambda (arg ...) expression).
If you take any Scheme expression like (if (positive? x) - +) and surround them with an extra pair of parenthesis you are calling the result as a procecdure: ((if (positive? -1) - +)) ; ==> 0
An if works like this:
(if predicate-expression
consequent-expression
alternative-expression)
If you need to nest you need to have the nested if as alternative expression.
(if predicate-expression
consequent-expression
(if predicate-expression2
consequent-expression2
alternative-expression))
If you nest there cond might be a better match. The expression above might be written:
(cond (predicate-expression consequent-expression)
(predicate-expression2 consequent-expression)
(else alternative-expression))
else has special meaning in a cond so it's not something that works on its own.

QuickSort Scheme

I need to implement the following quicksort:
(quick-sort pred lst)
lst is the list of numbers to be sorted
pred is the predicate by which the list is ordered, the signature of this predicate is:
(lambda (x y) …)
the code here is working but the problem here that when i get in lst the same number more then once im entering to infinite loop, after hours of debugging i cant find the problem or how to solve it.
(define (quick-sort pred lst)
;Pivot is 1st element of the list
(define (pivot lst)
(if (or (null? lst) (= 1 (length lst)))
'done
(car lst)))
partition get the pivot the list and the predicate and splitting it to two lists
(define (partition piv lst pred)
;predPos is the pred it slef and predNeg is the negative of the pred
(let* ((predPos (lambda (x) (pred x piv) ))
(predNeg (lambda (x) (if (pred x piv) #f #t)))
;Filtering the big list in to two lists
(p1 (filter predPos lst))
(p2 (filter predNeg lst)))
;Recursivly doing the qucicksort on each list. and joining them together.
(cond ((and (null? p1) (null? p2)) (cons piv ()))
((null? p1) (quick-sort pred p2))
((null? p2) (quick-sort pred p1))
(else (joiner (quick-sort pred p1) (quick-sort pred p2))))))
;Joining 2 lists together
(define (joiner p1 p2)
(cond ((null? p1) p2)
((null? p2) p1)
(else (cons (car p1) (joiner (cdr p1) p2)))))
;The main quicksort method () and list size one are sorted!
(let ((piv (pivot lst)))
(if (or (null? lst) (= 1 (length lst)))
lst
(partition piv lst pred))))
If you remove the pivot before partitioning, you are guaranteed to make progress at each recursive step. Without this measure the pivot will stick at the front of its partition and you won't get anywhere.

Higher-Order Procedure - pair construction (cons, car, cdr)

i need to create this procedures: my-cons, my-car, my-cdr in Scheme.
It should work like this:
(define p1 (my-cons 3 8))
(p1 #t)
3
(p1 #f)
8
(my-car p1)
3
(my-cdr p1)
8
now, I have only this:
(define my-cons
(lambda (x y)
(cons x y)
(let ((a (car (cons x y))))
(lambda (a)
(if (equal? a #f) y x)))))
but in this code i can't apply my-cons or my-cdr on defined p1
Can someone help me with this?
First, there's some extraneous code in my-cons which doesn't seem to belong here. This is enough:
(define my-cons
(lambda (x y)
(lambda (a)
(if a x y))))
(Also, you don't need to compare a boolean value with #t or #f — it's usable in if as it is.)
Now you have my-cons that returns a function which returns either x or y depending on its arugment. You can use that when implementing my-car and my-cdr:
(define my-car
(lambda (c)
(c #t)))
(define my-cdr
(lambda (c)
(c #f)))

Resources