scheme

How to sort disorder list of numbers in scheme

六月ゝ 毕业季﹏ 提交于 2019-12-11 13:16:57
问题 What it the proper way to sort a list with values in Scheme? For example I have the values which are not ordered: x1, x5, x32 .... xn or 3, 4, 1, 3, 4, .. 9 First I want to for them by increase number and display them in this order: x1, xn, x2, xn-1 or 1, 6, 2, 5, 3, 4 Any help will be valuable. 回答1: This is the same question you posted before, but with a small twist. As I told you in the comments of my answer, you just have to sort the list before rearranging it. Here's a Racket solution:

TypeError, the object is not applicable

こ雲淡風輕ζ 提交于 2019-12-11 13:06:43
问题 I wrote a relatively simple bank account function, however when I try to run it I get an TypeError and I'm not sure to why? It's straight out of SICP so the solution is readily available, I just want to understand why my answer produces that error. Any thoughts? Here is my code: (define (make-password-account balance password) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient Funds")) (define (deposit amount) (set! balance (+

Transpose list of tuples filling with empty lists

蓝咒 提交于 2019-12-11 12:34:00
问题 I'm new to Scheme and I'm trying to write a procedure which combines n list into a list of n -tuples. If the lists are of different size, the tuples should contain the empty list () when the corresponding list ran out of elements. My current implementation is the following: (define (comb list1 list2) (cond [(empty? list1) empty] [(empty? list2) empty] [else (cons (list (first list1) (first list2)) (comb (rest list1) (rest list2)))])) However, this program doesn't produce another tuple when

Naming variables using variables in Racket?

橙三吉。 提交于 2019-12-11 12:07:45
问题 If I have two variables, for example (define x 10) (define y 20) And I want to create a new variable, using the values of x and y to create the name, how would I go about doing so? For example let's say I want to make a new variable called variable-x-y (define variable-x-y "some-value") In this case, x would be 10 and y would be 20. Basically to summarize everything, I want to be able to enter variable-10-20 and have it return "some-value" I'm sorry if this sounds like a novice question. I'm

Creating an append function in Racket

五迷三道 提交于 2019-12-11 11:07:36
问题 In ISL, how would you create a recursive append function that takes two lists and returns a list of all highest position elements of the first list with the highest position elements of the second list (without using lambda or append )? Basically a function that would hold for these check expects: (check-expect (append-test '(a b c) '(d e f g h)) (list 'a 'b 'c 'd 'e 'f 'g 'h)) (check-expect (append-test '() '(7 2 0 1 8 3 4)) (list 7 2 0 1 8 3 4)) I feel like it would definitely use map ,

Remove subsequence function (deep recursion) in scheme

大憨熊 提交于 2019-12-11 10:45:01
问题 Im trying to write a function called removesub* which accepts two arguments (l1 and l2) . The function needs to return the second list with the first occurence of the subsequence removed. So, if the first list is '(a b c) , the first a if the second list is removed, the first b that appears after the removed a is removed, and the first c that appears after the removed b is removed - no matter how deep the atoms are nested. Working Example Input: (removesub* '(a b) '(w (x b) ((a) ((y z))) b a)

Scheme: Towers of Hanoi (recursion)

我只是一个虾纸丫 提交于 2019-12-11 10:43:12
问题 I'd like to start off by saying this is homework; so I'm not asking for a solution, just some tips. I've been ruminating on this for about a week now. Every solution I come up with doesn't do it recursively, seeing as I can't manage to wrap my head around doing it recursively with the list as the only parameter. My professor said they were able to do it with about 6 helper functions. As mentioned, I have to solve the problem taking taking the list as the only parameter. Here's an example of

Scheme: Recursion with list append

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 10:38:37
问题 I have a recursive function that basically keeps appending elements to a list recursively until a condition has been met. There's an issue though, and that's to use append , we must give it a quoted list. So doing (append (1 2) 3) gives us an error. The problem is when I first pass a list to the argument, I can put the ' to make it a quoted list. However, once I append something to that list and it gets recursively passed to the same function again, the second time append tries to work, it

Is there a way to change the values of variables in this language?

守給你的承諾、 提交于 2019-12-11 10:14:27
问题 I am writing a procedure in scheme and I am trying to manipulate the values of variables. I use the define function to give a value to a variable but I can't change the value. I would have used the let function but the variable change is only effective in the body of the let function. Are there other ways to manipulate variabes and be able to view the changes from anywhere in the procedure? Thanks 回答1: you can use set! set-car! set-cdr! after the variable has been defined 来源: https:/

Scheme Vector using merge sorting

白昼怎懂夜的黑 提交于 2019-12-11 10:06:31
问题 I have a vector, the elements of each vector is a list, I want to sort the elements regarding to the length of list. I am using this to sort my vector but I got the error (define vector-merge! (lambda (newvec vec left group-size vec-size) (let* ((top-left (min vec-size (+ left group-size))) (right top-left) (top-right (min vec-size (+ right group-size)))) (let mergeloop ((left left) (right right) (i left)) (cond ((and (< left top-left) (< right top-right)) (if (< (vector-ref vec left) (vector