logo
down
shadow

Does a non-null Scheme list contain at least one atom?


Does a non-null Scheme list contain at least one atom?

By : Beya Dachraoui
Date : November 22 2020, 04:01 AM
fixed the issue. Will look into that further lat is assumed to be a list of atoms at that point in the book.
If it's not empty, by definition it contains some atoms in it.
code :


Share : facebook icon twitter icon
check of the atom if it's the last element of the list in Scheme

check of the atom if it's the last element of the list in Scheme


By : aws Tamimi
Date : March 29 2020, 07:55 AM
I wish this help you im trying to solve a question which is to check weather the element x is the last element of the list , This would be the correct version:
code :
(define (last? elt lst)
  (cond
    ((null? lst)       #f)
    ((null? (cdr lst)) (eq? elt (car lst)))
    (else              (last? elt (cdr lst)))))
(define (last? elt lst)
  (if (null? lst)
      #f
      (eq? elt (car (reverse lst)))))
(define (last? elt lst)
  (and (not (null? lst))
       (eq? elt (car (reverse lst)))))
Adding an atom to a list in Scheme

Adding an atom to a list in Scheme


By : S.C.
Date : March 29 2020, 07:55 AM
With these it helps I want to add atoms to the end of a list and I also want the original list to change. I've written this small program, but it doesn't mutate the list. Why is that and how can I change it. PS: I'm new to scheme. , Just use a syntactic extension:
code :
(define-syntax-rule (append! l1 at)
  (set! l1 (append l1 (list at))))
> (define l1 '(1 2 3))
> (append! l1 4)
> l1
'(1 2 3 4)
Scheme: how to get first atom from list

Scheme: how to get first atom from list


By : Lukas Žvikas
Date : March 29 2020, 07:55 AM
will be helpful for those in need Recursion is not that different from the usual way yo solve problems - you just have to be careful and set some meaningful base cases. For instance, how would you solve this problem in Java? by traversing all the list, and stoping when either 1) we found the element that matches the condition or 2) there are no more elements. This is exactly what we need to do:
code :
(define (first-atom lst)
  (cond ((null? lst) #f)           ; there are no more elements in the list
        ((atom? (car lst))         ; current element is an atom
         (car lst))                ; return it
        (else                      ; otherwise
         (first-atom (cdr lst))))) ; look in the rest of the list
(first-atom '((2) 42 (3)))
=> 42
(first-atom '((1) (2)(3)))
=> #f
Scheme function - take an atom and a list and return new list where the given atom occurrences are removed from given li

Scheme function - take an atom and a list and return new list where the given atom occurrences are removed from given li


By : jenny
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You seem to be calling remover with swapped arguments. If you call it like that:
code :
(remover '(a f c g f b) 'f)
(define (remover lst n)
    (cond ((null? lst) ())
        ((eq? (car lst) n) (remover (cdr lst) n))
        (else (cons (car lst) (remover (cdr lst) n)))))
(define (remover n lst)
    (cond ((null? lst) ())
        ((eq? (car lst) n) (remover n (cdr lst)))
        (else (cons (car lst) (remover n (cdr lst))))))
Scheme function to take a list and an atom as parameters and returns index of the first location where atom occurs

Scheme function to take a list and an atom as parameters and returns index of the first location where atom occurs


By : Jon
Date : March 29 2020, 07:55 AM
hope this fix your issue I'm very new to Scheme and am working on a problem defined as follows: , An atom is defined by this predicate:
code :
(define (atom? x)
  (and (not (null? x))
       (not (pair? x))))
(define (find-loc lst atm)
  (cond ((not (list? lst)) 0)
        ((null? lst) 1)
        ((and (atom? (car lst)) (equal? (car lst) atm)) 1)
        (else (+ 1 (find-loc (cdr lst) atm)))))
shadow
Privacy Policy - Terms - Contact Us © bighow.org