Scheme evaluation order standard - scheme

I've got a program I'm writing for a class to substitute the left-most occurrence of a variable with a new variable. (It actually allows you to provide an equivalence relation yourself, as well). The thing is, in Chez Scheme 8.2, this substitutes the right-most occurrence, if the left most is inside a list. We use a server running some version of scheme (I'm not sure which version), and on the server it substitutes, correctly, the left-most occurrence. Below is the code:
(define subst-leftmost
(lambda (new old ls proc)
(let ([keep-going? #t])
(letrec ([helper
(lambda (ls)
(cond [(null? ls) ls]
[(or (pair? (car ls)) (null? (car ls)))
(cons (helper (car ls)) (helper (cdr ls)))]
[(and keep-going? (proc old (car ls)))
(set! keep-going? #f) (cons new (cdr ls))]
[else (cons (car ls) (helper (cdr ls)))]))]) (helper ls))))
This is called like so: (subst-leftmost 'x 'a '(d b c (a) b a) eq?) which should produce the output (d b c (x) b a), and does on the server. In Chez scheme, however, it produces (d b c (a) b x). I think the difference is due to the line
[(or (pair? (car ls)) (null? (car ls)))
(cons (helper (car ls)) (helper (cdr ls)))]
evaluating the helper of the car and the helper of the cdr in a not-set order.
My question is this: Which version of scheme is following the standard, and how can I modify my code so that it works correctly in both versions?
(I've already talked to my professor about this. He's going to address the class about it on Monday, once he can think about it some, but I'm curious. I also already got the full points for the assignment, so don't worry about the ethics of helping me, in that regard.)

There isn't any, sorry. Here's the relevant legalese. Use LETs or LET* if you need to evaluate sub-expressions in a particular order.

Scheme guarantees no specific order (as Cirno has said). If your code has no side-effects, this doesn't matter.
However, your code is side-effecting (because of the set! to an outside variable), so, you have some choices:
Use Racket (which is committed to using left-to-right order, last time I talked to a Racket dev)
Structure your code to remove side-effects, so that your helper function doesn't change any variable or state outside it
Use appropriate lets to ensure the ordering you need (as Cirno suggested); in particular, change (cons (helper (car ls)) (helper (cdr ls))) to:
(let ((depth-first (helper (car ls))))
(cons depth-first (helper (cdr ls))))

Related

Issue unbound variable in chez scheme

I am defining a function replaceOccurrence that replaces the first occurrence of either of two symbols. I don't understand why list variable is not bound
Chez Scheme Version 9.5
(define replaceOcurrence
(λ (new ocurrence1 ocurrence2 lista)
(cond
((null? lista) '())
(else (cond
((eq? ocurrence1 (car lista))
(cons new (cdr (lista))))
((eq? ocurrence2 (car lista))
(cons new (cdr (lista))))
(else (cons (car lista)
(replaceOcurrence new ocurrence1 ocurrence2 (cdr lista)))))))))
Exception: variable lista is not bound
Chez Scheme does not support λ as a replacement for lambda. Racket does, and says so explicitly in the documentation. Guile appears to support λ too, but I don't see that documented. Since λ is not recognized as a special form in Chez Scheme, the arguments to λ are evaluated first, and since they have not yet been bound, the reported error is issued.
There is another problem in the OP code: lista is alone in parentheses twice; this attempts to call lista as a procedure each time. Also, occurrence is spelled with two Cs; and prefer kebab-case to camelCase in Scheme. Here is the fixed code:
(define replace-occurrence
(lambda (new occurrence1 occurrence2 lista)
(cond
((null? lista) '())
(else
(cond
((eq? occurrence1 (car lista))
(cons new (cdr lista)))
((eq? occurrence2 (car lista))
(cons new (cdr lista)))
(else
(cons (car lista)
(replace-occurrence new occurrence1 occurrence2 (cdr lista)))))))))
The best, and most portable, way to solve OP problem is to simply use lambda. But, if one really wants to use λ, a macro can be used to provide the necessary syntax:
(define-syntax λ
(syntax-rules ()
[(_ formals . body)
(lambda formals . body)]))
With the inclusion of the above macro (and the other necessary changes mentioned in the first part of the answer), OP code can use λ in place of lambda.

Implementing Number Sort with Append Function in Scheme

I've been really stuck on implementing this function for some time, now, and cannot find the right resource to help me out further.
I am trying to implement a number sort, that sorts values in a list in ascending order.
My current implementation is:
(define num-sort
(lambda (ls)
(if(null? (cdr ls))
ls
(if(< (car ls) (cadr ls))
(append (car ls) (num-sort(cdr ls)))
(if(> (car ls) (cadr ls))
(append (num-sort(cdr ls)) (car ls))
(num-sort(cdr ls))
)))))
I've been having an EXTREMELY rough time with scheme and understanding logically how things are done.
My "Append" function is a function I implemented prior to this, and it appears to work as intended:
(define append
(lambda (ls1 ls2)
(if (null? ls1)
ls2
(if (pair? ls1)
(cons (car ls1) (append (cdr ls1) ls2))
(cons ls1 ls2)))))
I understand there is a problem with the recursion, here, in that when I compare two elements and decide to do another recursive call, it completely screws up the order entirely - however I'm not sure how to fix this.
In my head, what I'm thinking of doing is assembling a list by comparing each element to neighboring elements. Normally, I'd try doing this by storing and accessing indices of an array/variables in another language, but I can't wrap my head around how to sort properly in this one.
If you'd like to implement Selection Sort, here's a start:
(define (num-sort ls)
(if (null? ls)
ls
(num-sort-helper (car ls) '() (cdr ls))))
(define (num-sort-helper min-so-far checked unchecked)
# min-so-far is no bigger than anything in checked
# nothing in unchecked has been looked at
# return a list made up of min-so-far and the elements of
# checked and unchecked such that that first element returned
# is no bigger than anything else in that list
# For example, (num-sort-helper 5 '(6 9 7) '(8 2 3)) returns
# a list of all 7 numbers with 2 at the beginning
(cond ((null? unchecked) ???) # nothing is bigger than min-so-far
((<= min-so-far (car unchecked)) ???) # min-so-far is still the "winner"
(else ???))) # (car unchecked) is now the "winner"

checking the first atom against the other atoms in a list

I am trying to check the first atom in a list against the other atoms in a list, so if the first atom is 3 and the 3rd atom is three, I would want to evaluate it to false.
I have something like
(define (doubles a_list)
(cond
((null? a_list) #t)
(not ((eq? (car a_list) (car (cdr a_list)))) (doubles(cdr a_list)))
I have a feeling that this will only check atoms next to each other and not the one atom with the rest of them. what can i do to check all the items with the first one?
You want to repeatedly compare the first two elements of your list, reducing the cdr as you go:
(define (unique-first? x)
; uncomment the following line to trace the execution
;(display x) (newline)
(or (null? x) (null? (cdr x))
(and (not (eq? (car x) (cadr x)))
(unique-first? (cons (car x) (cddr x))))))
note on special cases:
(unique-first? '()) -> #t
(unique-first? '(a)) -> #t
(which, indeed, verify the "first element doesn't appear twice" criteria).
I've got my tea, my code-review hat and a few spare minutes. You know what time it is.
What you want is a predicate that will tell you if there are duplicates of the first atom in a list.
(define (doubles a_list)
(cond ((null? a_list) #t)
(not ((eq? (car a_list) (car (cdr a_list)))) (doubles(cdr a_list)))
Regardless of anything else, this won't work because it has unbalanced parentheses.
(define (doubles a_list)
(cond ((null? a_list) #t)
(not ((eq? (car a_list) (car (cdr a_list)))) (doubles(cdr a_list)))))
That revised version won't work because it has a malformed second clause in the case. Oddly, it does seem to evaluate fine, but when you call it, you'll get an odd error message
Welcome to Racket v5.3.6.
> (define (doubles a_list)
(cond ((null? a_list) #t)
(not ((eq? (car a_list) (car (cdr a_list)))) (doubles(cdr a_list)))))
> (doubles (list 1 2 3 4 5 6 7 3))
application: not a procedure;
expected a procedure that can be applied to arguments
given: #f
arguments...: [none]
>
The direct cause is this bit
... ((eq? (car a_list) (car (cdr a_list)))) ...
Because of the extra parens surrounding this expression, what it actually means is
"See if the first element of a_list is the same as the second, then call the result of that check as a function".
This is not what you want. The correct resolution is getting that not into these parens, which would make that a valid cond clause.
(define (doubles a_list)
(cond ((null? a_list) #t)
((not (eq? (car a_list) (car (cdr a_list))))
(doubles (cdr a_list)))))
You can't call car on the empty list in Scheme. This works fine, for some definition of "fine", in Common Lisp but will throw you an error here.
> (define (doubles a_list)
(cond ((null? a_list) #t)
((not (eq? (car a_list) (car (cdr a_list))))
(doubles (cdr a_list)))))
> (doubles (list 1 2 3 4 5 6 7 3))
car: contract violation
expected: pair?
given: '()
>
The cause of this error is that you're checking whether a_list is null?, but then calling (car (cdr a_list)) later on. You'll get this error in the situation where a_list is something like (3). The fix for this is checking whether a_list or its cdr are null?
(define (doubles a_list)
(cond ((or (null? a_list) (null? (cdr a_list))) #t)
((not (eq? (car a_list) (car (cdr a_list))))
(doubles (cdr a_list)))))
> (doubles (list 1 2 3 4 5 6 7 3))
#t
Now that we've got a version of your function that doesn't error out, lets take a look at your logic. The procedure for finding doubles in a list is
for lists of length zero or one, the answer is False, since there can be no doubles in a list shorter than two elements
otherwise, check if the first element of the list is in the rest.
if it is, the answer is True since we've found a duplicate
Now, you've named your function doubles, but your prose explanation tells me that it really should have been unique-first?. Because you're not looking for doubles, your're looking to check if the first element of your list is unique among its peers. What you really want to do is
for lists of length one, the answer is True, since that single element must be unique (I'm going to assume that you want the same for lists of length zero, but that might not actually make sense depending on the application)
otherwise, check that the first element of the list is not in the rest.
That translates to
(define (unique-first? a_list)
(if (or (null? a_list) (null? (cdr a_list)))
#t
(not (member? (car a_list) (cdr a_list)))))
The member? function is fairly straightforward, and works on the same principles.
(define (member? elem lst)
(cond ((null? lst) #f)
((eq? elem (car lst)) #t)
(else (member? elem (cdr lst)))))
Finally, a couple of style points. The Scheme convention is to name predicates with a trailing ?, which will hint to your functions' callers that it will return #t or #f (I've done this above), and to use spinal-case rather than snake_case for names.
(define (unique-first? a-list)
(if (or (null? a-list) (null? (cdr a-list)))
#t
(not (member? (car a-list) (cdr a-list)))))

Detecting the caller of a function in Scheme or Racket

In Scheme or Racket is it possible to detect the caller of a function?
For example, I can write a function to test if a list is a list of atoms as follows:
(define atom? (lambda (x) (and (not (pair? x)) (not (empty? x)))))
(define lat? (lambda (l)
(define latt?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l)) (latt? (cdr l)))
(else #f))))
(if (null? l) #f (latt? l))))
but instead of the above, is there a function like "called-by" to do something like this:
(define lat?
(lambda (l)
(cond
((and (null? l) (called-by "lat?")) #t)
((atom? (car l)) (lat? (cdr l)))
(else #f))))
The usual way to do this is to add some argument to the function, or make a loop via an internal definition as you did. Other than that, there is no reliable way to find out the caller of a function.
But in your case, it seems like a good lack of feature -- using it for the above problem is pretty bad. There's nothing wrong with the internal helper version. (It's also quite similar to any other language.)
Finally, I'd expect (lat? null) to return #t since it is a list that has only atoms as elements.

Scheme Deep reverse function

I am doing a scheme program which takes in a list and then reverse it. So far it works for simple list, which does not contain any sublist, but when I test it for a list contains sublist, it fails. Please help me where is wrong with it.
Here is the code:
(define deep-reverse
(lambda (L)
(cond
((empty? L) '())
(else (append (deep-reverse (rest L)) (list (first L)))))))
(define (deeprev L)
(if (null? L) '()
(if (list? (car L))
(if (chek (car L)) (append (deeprev (cdr L)) (list (reverse (car L))))
(append (deeprev (cdr L)) (list (deeprev (car L)))))
(append (deeprev (cdr L)) (list (car L))))))
First of all, you are using undefined Scheme functions. I am going to work off the following assumptions:
empty? is null?
rest is cdr
first is car
Your code works by taking the first element in a list and adding it to another list. However, that first element of a list can be a list itself. You need to test to see if the element that you're working on is atomic or a list. If it's a list, then you call deep-reverse recursively.
If you would like to see code appended to this, leave a comment.

Resources