scheme

scheme determine a string inside a tree list or not

戏子无情 提交于 2019-12-13 04:48:18
问题 Here's my data definition, (define-struct leaf ()) ;; interpretation: represents a leaf on a BT, a node with no children (define-struct node (word left right)) ;; interpretation: represents a node on a BT with a word, a left and a right ;; subtree ;; A BinaryTree (BT) is one of ;; - (make-leaf) ;; - (make-node String BT BT) ;; bt-has? : BT String -> Boolean ;; given a BT tree and a String w and returns true if w is in the tree ;; and false otherwise (define (bt-has? tree w) (cond [(leaf? tree

How does c++ for loop change to scheme

醉酒当歌 提交于 2019-12-13 04:40:53
问题 c++ int loop(int x, int y, int z) { int result = 0; for ( int i = x; i < y; i+=z ) { result += i; } return result; } Just i try that by scheme (letrec ((my-loop (lambda (a b c) (begin (let ((i a) (s 0)) (if (< i b) (set! s (+ s i)) s))))))(my-loop (+ a c) b c)) please write correct code of scheme.... 回答1: Here's a straightforward translation to a do loop: (define (foo x y z) (do ((result 0 (+ result i)) (i x (+ i z))) ((>= i y) result))) However, many Schemers find do loops to be distasteful.

Recursive Multiplication in Scheme (trouble with negatives)

丶灬走出姿态 提交于 2019-12-13 03:35:31
问题 I am trying to multiply in Scheme using recursion, and I am able to do so with positive numbers but not negatives (they get stuck in an infinite loop). I assume the problem comes in the 3rd and 4th lines, which I wrote in an attempt to be able to handle negatives. Any help would be appreciated :) (define Multiply(lambda (x y) (if (eq? x 0) 0 (if (eq? y 0) 0 ;if either x or y are zero, return 0 (if (> 0 x) (- 0 (+ x (Multiply x (- y 1)))) (if (> 0 y) (- 0 (+ x (Multiply x (- y 1)))) (+ x

Insert-everywhere procedure

痞子三分冷 提交于 2019-12-13 03:00:56
问题 I am trying to write a procedure that takes a a symbol and a list and inserts the symbol at every possible position inside the given list (thus generating a list of lists). I have coded the following definitions, which I must implement: 1 (define (insert-at pos elmt lst) (if (empty? lst) (list elmt) (if (= 1 pos) (cons elmt lst) (cons (first lst) (insert-at (- pos 1) elmt (rest lst)))))) 2 (define (generate-all-pos start end) (if (= start end) (list end) (cons start (generate-all-pos (+ start

find the max of Collatz sequence from a giving list

半腔热情 提交于 2019-12-13 02:46:23
问题 i'm new in scheme syntax. this is the last part of the project i've been working on. i was able to find the max from a giving Collatz sequence, but this part of the project requires finding the max length from a multiple Collatz sequences lists. So for example giving this list : '((1 10) (10 200) (201 210) (900 1000) and the output should be like this : ‘(20 125 89 174) i need to find the maximum length between the number 1 to 10 and then from 10 to 200 ets here is my code : #lang racket ;

Finding the diagonals of square matrix made from lists

巧了我就是萌 提交于 2019-12-13 02:32:24
问题 I am currently trying to define a function that takes a list of lists, models that list as a square matrix and returns the diagonal of said matrix. For example, input ((a b c) (d e f) (g h i)) gives (a e i) . I have a vague idea of how to go about solving this (taking the last element of the last list then the second to last element of the second to last list etc.) but I am not really sure of how to go about programming this in Scheme. I would appreciate it if somebody could point me in the

Pass by value confusion in Scheme

末鹿安然 提交于 2019-12-13 02:27:45
问题 Consider the following procedure taken from SICP: (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) Suppose I say: (define x (make-withdraw 100)) make-withdraw returns a procedure ( (lambda (amount) ... ) ) inside a new environment called e2 (enclosing the binding for the variable balance ), and binds that procedure to x in the global frame. Now, say I call: (f x) where (define (f y) (y 25)) 1 . I

Scheme Find all possible paths for an undirected graph

╄→гoц情女王★ 提交于 2019-12-13 02:19:51
问题 I am having problem to print out all the possible paths. Currently I am only able to print out one path, and if (path-demo "J" "I"), the program will shown this error mcdr: expects argument of type <mutable-pair> ; given #f (define net '(("A" "B") ("B" "A" "C") ("C" "B" "D") ("D" "C" "E" "F") ("F" "I" "D") ("I" "F") ("E" "D" "J") ("J" "E" "G") ("G" "J" "H"))) (define (path-demo start finish) (for-each (lambda (x) (display x) (display " ")) (cons "Route:" (shortest-path start finish net))))

Power set in Scheme with ordered output

人盡茶涼 提交于 2019-12-13 00:39:09
问题 So I am familiar with the algorithm for creating a power set using Scheme that looks something like this: (define (power-set set) (if (null? set) '(()) (let ((power-set-of-rest (power-set (cdr set)))) (append power-set-of-rest (map (lambda (subset) (cons (car set) subset)) power-set-of-rest))))) So this, for (1, 2, 3, 4), would output: (() (4) (3) (3 4) (2) (2 4) (2 3) (2 3 4) (1) (1 4) (1 3) (1 3 4) (1 2) (1 2 4) (1 2 3) (1 2 3 4)) I need to figure out how to output the power set "in order",

Is it possible to implement the Yin-Yang puzzle in C# 5.0 using async?

隐身守侯 提交于 2019-12-13 00:36:12
问题 The puzzle, implemented in Scheme, is as follows: (let* ((yin ((lambda (cc) (display #\@) cc) (call-with-current-continuation (lambda (c) c)))) (yang ((lambda (cc) (display #\*) cc) (call-with-current-continuation (lambda (c) c))))) (yin yang)) The goal of the puzzle is to work out and understand the output of this code. I'm wondering if it's possible to implement code with the same semantics using C# 5.0's new async CPS features. The part that I'm having trouble grasping, is that the puzzle