scheme

How can write a program in scheme to find factors of a list of numbers

倖福魔咒の 提交于 2021-02-05 12:22:35
问题 This is the code for a single integer, how can it extends to list of function? (define (factors n) (define (*factors d) (cond ((> d n) (list)) ((= (modulo n d) 0) (cons d (*factors (+ d 1)))) (else (*factors (+ d 1))))) (*factors 1)) (display (factors 1111111)) (newline) 回答1: You can use for-each to iterate over a list. (define (factors n) (define (*factors d) (cond ((> d n) (list)) ((= (modulo n d) 0) (cons d (*factors (+ d 1)))) (else (*factors (+ d 1))))) (*factors 1)) (define arbitarily

How to find the maximum nesting depth of a S-expression in scheme?

拈花ヽ惹草 提交于 2021-02-05 07:33:20
问题 for example (nestFind '(a(b)((c))d e f)) => 3 (nestFind '()) => 0 (nestFind '(a b)) => 1 (nestFind '((a)) )=> 2 (nestFind '(a (((b c d))) (e) ((f)) g)) => 4 this is what i tried so far but its not working properly: (define (nestFind a) (cond ((null? a)0) ((atom? a)0) ((atom? (car a))(+ 1 (nestFind (cdr a)))) (else (+(nestFind (car a))(nestFind (cdr a)))))) 回答1: It's a bit simpler. Give this a try: (define (nestFind lst) (if (not (pair? lst)) 0 (max (add1 (nestFind (car lst))) (nestFind (cdr

What is a smallest set of indices that allows to fully bind any pattern of 6-tuple in one hop?

不问归期 提交于 2021-02-04 21:13:20
问题 I am trying to build a 6-tuple store on top of wiredtiger. The tuples can be described as follow: (graph, subject, predicate, object, alive, transaction) Every tuple stored in the database is unique. Queries are like regular SPARQL queries except that the database store 6 tuples. Zero of more elements of a tuple can be variable. Here is an example query that allows to retrieve all changes introduces by a particular transaction P4X432 : SELECT ?graph ?subject ?predicate ?object ?alive WHERE {

why does apply map and to list of expressions returns only one boolean?

笑着哭i 提交于 2021-01-29 05:45:50
问题 (define (cart . lists) (cond ((null? lists) '()) ((= (length lists) 1) (map list (first lists))) (else (append-map (lambda(x) (map (lambda(y) (cons y x)) (first lists))) (apply cart (rest lists)))))) (define (numbers n) (define (reversed-numbers n) (if (= n 0) '() `(,n . ,(reversed-numbers (- n 1))))) (reverse (reversed-numbers n))) (define (gen-truth n vals) (apply cart (map (lambda(x) list vals) (numbers n)))) (gen-truth 2 '(#t #f)) returns: '((#t #t) (#f #t) (#t #f) (#f #f)) (define and-l

Why does this return a list '(5) rather than the number 5?

放肆的年华 提交于 2021-01-28 12:01:02
问题 I am working through SICP, and the exercise I am working on asks for a procedure that returns the last element in a list. I implemented the procedure last-pair to do this, but I'm confused why it's returning a list rather than a number: (define (last-pair alist) (cond ((null? (cdr alist)) (car alist)) ; still happens if this is just "car alist)" (else (last-pair (cdr alist))))) When I invoke it on a list of the integers from 1 to 5, I get the output '(5): > (last-pair (list 1 2 3 4 5)) '(5) I

Scheme: Is it possible to convert a list of S-expressions into a list of atoms?

*爱你&永不变心* 提交于 2021-01-28 11:26:14
问题 I am trying to convert a list of S-expressions to a plain list of atoms similar to a problem in the book The Little Schemer . My code is (as typed in Dr.Racket): > (define lat '((coffee) cup ((tea) cup) (and (hick)) cup)) > (define f (lambda (lat) (cond ((null? lat) (quote ())) ((atom? (car lat)) (cons (car lat) (f (cdr lat)))) (else (cons (f (car lat)) (f (cdr lat))))))) > (f lat) '((coffee) cup ((tea) cup) (and (hick)) cup) The above code is returning back the list the same as input list. I

Why does this not evaluate in Scheme?

元气小坏坏 提交于 2021-01-28 05:40:48
问题 I am using the DrRacket environment to try out the Scheme language. I defined sum+1 as follows: (define sum+1 '(+ x y 1)) I was wondering why the following expression does not evaluate: (let ([x 1] [y 2]) (eval sum+1)) whereas doing this returns the correct value: (define x 1) (define y 2) (eval sum+1) 回答1: eval does not work with lexical variables at all unless the lexical variable was created in the same expression: #!r7rs (import (scheme base) (scheme eval)) (define env (environment '

Why does the Racket interpreter write lists with an apostroph before?

拟墨画扇 提交于 2021-01-28 04:49:02
问题 Why is '(1 2 3) written instead of (1 2 3) ? > (list 1 2 3) '(1 2 3) 回答1: Racket's default printer prints a value as an expression that would evaluate to an equivalent value (when possible). It uses quote (abbreviated ' ) when it can; if a value contains an unquotable data structure, it uses constructor functions instead. For example: > (list 1 2 3) '(1 2 3) > (list 1 2 (set 3)) ;; sets are not quotable (list 1 2 (set 3)) Most Lisps and Schemes print values using the write function instead.

Sorting list of lists by their first element in scheme

北城以北 提交于 2021-01-27 21:22:52
问题 I'm working on sorting a list of lists by their first element for example (sort (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 1)))) expected output => ('(1 1) '(2 1 6 7) '(4 3 1 2 4 5)) The algorithm I used is bubble sort. And I modified it to deal with lists. However, the code doesn't compile. The error is mcar: contract violation expected: mpair? given: 4 Can someone correct my code and explain it. Thank you (define (bubble L) (if (null? (cdr L)) L (if (< (car (car L)) (car (cadr L))) (list (car L)

On Scheme cons and dots notation

好久不见. 提交于 2021-01-27 17:28:19
问题 Given #;> (cons (cons 1 2) 3) ((1 . 2) . 3) When we try #;> (cons 3 (cons 1 2)) (3 1 . 2) What governs where the . is used? What would the memory representation of these constructs be? 回答1: Scheme implementations usually print things that look like lists in list form: -> (cons 1 (cons 2 '())) '(1 2) In your example, (cons 3 (cons 1 2)) would be a list if it weren't for the last 2 . So your implementation makes a best effort to print it as a list until the 2 . The other example does not