scheme

Inserting into a Binary Tree in Scheme

青春壹個敷衍的年華 提交于 2019-12-11 03:35:31
问题 I am wondering how to insert elements from a list into a binary search tree. I am wondering why the following code does not work as I expected. The output is '((4 1 5 13 6) () ()) My next problem is going to be sorting the elements in the list, but for now I just want to insert them. Is my output correct for the problem I stated? My code is as follows: ( define ( make-tree value left right ) ( list value left right )) ( define ( value tree ) ( car tree )) ( define ( left tree ) ( cadr tree ))

Language Scheme: find the sum of proper divisors

会有一股神秘感。 提交于 2019-12-11 03:19:57
问题 I am wondering how to write a function calculating the sum of proper divisors of a integer greater than 1. (define (sum-of-proper-divisors n) (cond [(= n 1) 1] [(= 0 (remainder n (sub1 n))) (+ (remainder n (sub1 n)) (sum-of-proper-divisors (sub1 (sub1 n))))] [else (sum-of-proper-divisors (sub1 n))])) This is the code that I wrote, however, it does not work. It will never stop evaluating because it will always do n-1. And I don't know how to fix this. Also, there might be other problems. How

Detecting #<unspecified> in Scheme list

人盡茶涼 提交于 2019-12-11 03:13:16
问题 I have a function which return a list of values. Some of these values may be empty lists themselves, while some are not. However, at the end of every list, there is a #<unspecified> value present. I understand that this value is returned when the function does not return anything. I want to trim this value, along with other null lists. My list is like this: (() () MD- MC+. #<unspecified>) I intend to apply a filter function to this list. The criteria that I will be applying is null? . However

How to split a list into two parts in Scheme

笑着哭i 提交于 2019-12-11 03:04:07
问题 Example: (split '(1 2 3 4) '3) the Answer should be: ((1 2 3) 4) The function required 1 list and 1 number, the output should be nested list the nested list consist of all elements of "mylist" which are equal or less than the "num", and the greater number should be on the right of the list. I tried but out put is only one list: (define (split mylist num) (cond ((null? mylist)'()) ((list? (car mylist))(split(car mylist) num)) ((> (car mylist) num)(split(cdr mylist) num)) (else(cons (car mylist

How to find “the first survivor” after a given position in the Josephus puzzle?

只愿长相守 提交于 2019-12-11 02:53:56
问题 I want to find the next survivor after a given position and number of people. (define renumber (lambda (position n) (if (< position 3) (+ position (- n 3)) (- position 3)))) (define survives? (lambda (position n) (if (< n 3) #t (if (= position 3) #f (survives? (renumber position n) (- n 1)))))) (define first-survivor-after (lambda (position n) (cond ((and (<= n 3)(<= position 3)) null) ((or (>= n 3)(>= position 3))(survives? position n) (if = #f survives?) (survives? (+ 1 position) n)

How to control order of Scheme macro expansion?

夙愿已清 提交于 2019-12-11 02:35:57
问题 I'm working with the Racket macro extension syntax-id-rules , that some other Scheme implementations provide under the name identifier-syntax . These let you specify macro expansions that will happen even when the defined identifier isn't in head position. So for example: (define hidden #f) (define-syntax proxy (syntax-id-rules (set!) [(set! proxy v) (set! hidden v)] [proxy hidden])) will set up the identifier proxy to be a proxy for hidden . This is a useless example, but it illustrates the

How to write to a file in tinyscheme?

ぃ、小莉子 提交于 2019-12-11 02:25:54
问题 Scheme implementation : tinyscheme Here is my try: (with-output-to-file "biophilia.c" (lambda (output-port) (write "Hello" output-port))) Ceates biophilia.c with following content: Error: ( : 26) not enough arguments What am I doing wrong here? how to repair it? (define (with-output-to-file s p) (let ((outport (open-output-file s))) (if (eq? outport #f) #f (let ((prev-outport (current-output-port))) (set-output-port outport) (let ((res (p))) (close-output-port outport) (set-output-port prev

merging two lists positionally in Scheme

…衆ロ難τιáo~ 提交于 2019-12-11 02:20:58
问题 Example First list: (1 2 3 4) Second list: (* slot (- slot (/ slot slot))) Output: (* 1 (- 2 (/ 3 4))) The first list's elements will be inserted to the second list. The symbol slot in second list means the position for insertion. My Solution I have written a code snippet and it works for the above example. (define (insert-slot numbers slots) (cond [(null? slots) '()] ;; operate on the nested list and come back to the element after [(pair? (car slots)) (cons (insert-slot numbers (car slots))

How can find all functions and bounded symbols in an “environment”

三世轮回 提交于 2019-12-11 02:19:23
问题 I am using a softer who has a build-in scheme interpreter. I know the "environment" name is (the-environment). How can I find all the functions and symbols in the environment ? (define p (open-output-file "d:/test.txt")) (display (the-environment) p) can this will display all the functions ? Thanks in advance. Joe 回答1: As Eli Barzilay pointed out, whether or not you can reflectively find all the names bound in an environment depends on which implementation of Scheme you are using. I infer

How to store a function in a variable in Lisp and use it

爷,独闯天下 提交于 2019-12-11 02:02:06
问题 I want to store a function like print in a variable so that I can just type something short like p , e.g: In Scheme : (define print display) (print "Hello world\n") ;; alternate way (define print 'display) ((eval print) "Hello world\n") The same approach does not seem to work in Common Lisp : (defvar p 'print) ;;(print (type-of p)) (p "Hello world") ;; Attempt 1 ((eval p) "Hello world") ;; >> Attempt 2 ((eval (environment) p) "Hello world") ;; Attempt 3 I'm getting this error with Attempt 1