scheme

rearrange elements in a List using Scheme

♀尐吖头ヾ 提交于 2019-12-24 14:07:13
问题 I am trying to write a code using SCHEME that takes two arguments, for example '(2 1 3) & '(a b c) and gives a list '(b a c). My code is not working either recursive or iterative. Any help!! (define project (lambda (list1 list2 list3 n b index) (define n (length(list1))) (let ((i n)) (for-each (i) (cond ((null? list1) (display "empty")) (else (define n (car list1)) (define index (- n 1)) (define b (list-ref list2 index)) (define list3 (cons list3 b)) (define list1 (cdr list1)) list3 ))))))

Insert node key and value in binary search tree using scheme

こ雲淡風輕ζ 提交于 2019-12-24 13:33:46
问题 I am inserting a key key and a value val into the tree-map t returning a new tree with a node containing the key and the value in the appropriate location. I have this defined: (define (tree-node key value left right)(list key value left right)) (define (get-key tn) (node_key tn)) (define (get-val tn) (node_val tn)) (define (get-left tn) (node_left tn)) (define (get-right tn) (node_right tn)) (define (node_key tn) (list-ref tn 0)) (define (node_val tn) (list-ref tn 1)) (define (node_left tn)

Update the whole structure

十年热恋 提交于 2019-12-24 10:48:13
问题 Suppose I have some function which returns a struct: (struct layer (points lines areas)) (define (build-new-layer height) ... (layer list-a list-b list-c)) I want to keep track of the last returned result something like: (define top-side (build-new-layer 0)) ; store the first result ... (set! top-side (build-new-layer 0.5)) ; throw away the first result and store the new one However, for that particular code I get the error: set!: assignment disallowed; cannot modify a constant constant: top

SCHEME - Writing my own append produces a weird result

半城伤御伤魂 提交于 2019-12-24 10:44:58
问题 I want to write my own append , for appending an element to an existing list . I've written the following : (define (appendElem llist elem) (if (null? llist) elem (cons (car llist) (appendElem (cdr llist) elem)))) But when I do this : (appendElem (list 1 2 30) 11) I get : (1 2 30 . 11) So the question is , why (1 2 30 . 11) and not (1 2 30 11) ? Thanks EDIT: Fixed : (define (appendElem llist elem) (if (null? llist) (list elem) (cons (car llist) (appendElem (cdr llist) elem)))) 回答1: Think

How to erase all duplicate elements in a list Scheme?

两盒软妹~` 提交于 2019-12-24 09:20:58
问题 My attempt was, (define (remove-dup lst) (cond ((null? lst) '()) ((null? (cdr lst)) (car lst)) ((equal? (car lst) (car (cdr lst))) (remove-dup (cdr lst))) (else (cons (car lst) (remove-dup (cdr lst)))) ) ) My list was (a b c a a c c c ) What I want is (a b c) . Any idea? Thanks, 回答1: I'd approach this by looping with a second list that you build up of seen elements. I'll feel bad for giving this to you if this was homework though - it's more important to understand how recursion works than to

for loop in scheme

我只是一个虾纸丫 提交于 2019-12-24 08:34:26
问题 i'm kinda of confused how i can construct a for loop in scheme. the for-loop should be implemented in Part II. where it takes a list of numbers and insert each element inside the list in Part I to find the length. I was cable to get the first element but i need a for loop or what so ever to get an output like this: '(7 10 5 16 106 37) here is my code : #lang racket ; Part I (define (sequence n) (cond [(= n 1) (list n)] [(even? n) ( cons n(sequence( / n 2)))] [(odd? n) ( cons n(sequence (+(* n

Case Statement Not Assigning Value

一笑奈何 提交于 2019-12-24 08:18:52
问题 I'm having some trouble debugging a case statement. I was hoping that the statement would assign numeric values to note-val , but so far it is assigning #<void> . I know it's something wrong with the case statement, because if I add an else clause, that value gets applied. Given a sample input of '(((#\3 #\A) (#\4 #\B)) ((#\4 #\C))) , what am I doing wrong here? (In regards to the case statement. I'm sure there are other errors, but I'd like to try to work those out myself if I can get this

Scheme streams and circular lists

﹥>﹥吖頭↗ 提交于 2019-12-24 08:03:58
问题 In Scheme/Lisp I am trying to make a function that converts a list into a circular list. Therefore, I believe I need to construct an infinite stream in which the tail of the list points to the head of the list. Here is my code so far: (define (rotate-list l1 l1copy) (if (null? (force (cdr l1))) (cons (car l1) (delay l1copy))) (cons (car l1) (delay (rotate-list (force (cdr l1)) l1copy)))) All help is greatly appreciated. 回答1: No, you don't need streams to make a circular list. There are two

Making a random list in Scheme

…衆ロ難τιáo~ 提交于 2019-12-24 07:54:49
问题 I am just trying to randomly make a list and use it in a larger function. (define make-random-list (if (= (random 2) 0) (list 2 3) (list 3 2))) This only produces the list (2 3) and I am not sure why. What is happening to cause this? I can make the function work if I write it like this (define make-random-list (lambda (x) (if (= (random x) 0) (list 2 3) (list 3 2)))) and calling (make-random-list 2) but I do not understand why that would work and the other one would not. What is going on with

scheme: return a lst that only contains the first element of the lst

好久不见. 提交于 2019-12-24 06:48:35
问题 Here is the question: Write a function (first-n-elements lst n) , which returns a list containing only the first n elements of lst . For example, (first-n-elements '(1 2 3 4 5 6) 3) should return '(1 2 3) . Your function should handle the case where n is greater than the length of the list (in which case it would return the entire list), and where n is 0 (should return '() ). My answer is: (define (first-n-elements lst n) (cond((null? lst) '()) ((= n 0) lst)) ((> n 0) (cons (+ (car lst) 1)