scheme

Scheme changing tree values

大憨熊 提交于 2019-12-02 09:44:04
I need to implement a procedure called inverse-tree that receives a tree whose nodes data values are numbers and booleans and returns the equivalent tree whose nodes satisfy the following: If the equivalent node of the original tree is a number, then the resulting tree’s node is −1· that node value If the equivalent node of the original tree is a boolean, then the resulting tree’s node is the logical not of that node value Examples: > (inverse-tree ’()) ’() > (inverse-tree ’(5)) ’(-5) > (inverse-tree ’(0)) ’(0) > (inverse-tree ’(#f)) ’(#t) > (inverse-tree ’(#t)) ’(#f) > (inverse-tree ’(-5 (1 (

Converting numbers to english letter list

谁都会走 提交于 2019-12-02 09:42:53
I have the function below which converts an input of numbers into the partially translated word output of those numbers. Using product and quotient, it adds the word representation of numbers while splitting the number into groups. For example: (number-name 87969087) -> '(87 million 969 thousand 87) (number-name 1000000) -> '(1 million) Im trying to complete my problem by fully translating those numbers which are less than 1000 as well. Im trying to implement a function less-than-1000 which will display those smaller numbers as the list is being constructed as well. Alongside: ;; for less than

Removing all duplicate members from a list in scheme

吃可爱长大的小学妹 提交于 2019-12-02 09:25:47
I am trying to remove duplicates in a list, using recursion. This is what I have. It only removes the first duplicate, not all of them. My idea is to look at the first member, check if its a member of the rest of the list, if so, call the function again. If not, create a list with the first member and the result from calling the function again. I don't understand why it doesn't remove all the duplicates. (define (removeDupes L) (cond ((null? L) ()) ((list? (member (car L) (cdr L))) removeDupes (cdr L)) (#T (cons ((car L) (removeDupes (cdr L))))))) This is what I modified it to, and it works!!

scheme word lists eq?

不问归期 提交于 2019-12-02 09:23:41
i've got a problem: I need to find if list equal to the second one, for example: (set%eq? '(1 2 3) '(1 2 3)) ===> #t (set%eq? '(1 2 3) '(2 3 4)) ===> #f That examples are correct in my program, but this one is not: (set%eq? (quote ((quote one) (quote two) (quote three))) (quote ((quote one) (quote two) (quote three)))) ====> #f but i need #t what's wrong? this is my program: (define (set-eq? xs ys) (cond ((and (null? xs) (null? ys)) #t) ((null? ys) #f) ((eq? (car xs) (car ys)) (set-eq? (cdr xs) (cdr ys))) ((eq? (car xs) (car (reverse ys))) (set-eq? (cdr xs) (cdr (reverse ys)))) (else #f)))

Transform a natural number to a specific base and return it as a list

此生再无相见时 提交于 2019-12-02 09:07:56
I want to show the result of my function as a list not as a number. My result is: (define lst (list )) (define (num->base n b) (if (zero? n) (append lst (list 0)) (append lst (list (+ (* 10 (num->base (quotient n b) b)) (modulo n b)))))) The next error appears: expected: number? given: '(0) argument position: 2nd other arguments...: 10 I think you have to rethink this problem. Appending results to a global variable is definitely not the way to go, let's try a different approach via tail recursion: (define (num->base n b) (let loop ((n n) (acc '())) (if (< n b) (cons n acc) (loop (quotient n b)

Reverse list in Racket in O(n)

本小妞迷上赌 提交于 2019-12-02 09:04:19
I need to write a recursive function in Scheme which takes a list of atoms and reverses it in linear time. I am only allowed to use define, lambda, cons, car, cdr, cond, let, and null? . Here is what I have so far: (define reverse (lambda (lat) (cond ((null? lat) lat) (else (cons (reverse (cdr lat)) (cons (car lat) '())))))) So when I call the function: (reverse '(a b c d)) I get the following output: '(() (((() 4) 3) 2) 1) Any help would be very much appreciated. The problem is that if (reverse (cdr lat)) returns a list eg (3 2) then (cons '(3 2) (1)) turns into ((3 2) 1) . A classical way to

In Racket, if an unquoted pair is constructed with the dot notation, is it possible to use a variable or an expression value for the second element?

≯℡__Kan透↙ 提交于 2019-12-02 08:53:23
问题 In Racket, the following works: (+ . [1 2]) ; => 3 { define a + } (a . [1 2]) ; => 3 However, i see no way to define b to be the (1 2) list so as to get (+ . b) and (a . b) to return 3 . Is it possible? 回答1: Sure, just use apply : (define a +) (define b '(1 2)) (apply a b) ; => 3 (apply + b) ; => 3 回答2: How about this ... without using apply but using eval . But seriously, using apply is a better idea in this case, there's nothing wrong with it ( eval is evil though, see the documentation to

scheme sort list diffent criteria

本小妞迷上赌 提交于 2019-12-02 08:38:44
I have a finite list of quadruples, e.g. (list (list 1 3 5 5) (list 2 3 4 9) (list 3 4 4 6)(list 4 7 10 3)). I denote each of the elements by (a1 a2 a3 a4). Please help me to write a sorting function which provides a "increasing" list created according to the following criteria: the numbers a2, later the difference (a3 - a4), and later the numbers a3. Please help if you can. As far as I can tell, your ordered criteria are the order in which to sort. If this is the case, then the following program should perform that sorting. (define (strange-sort quadruples) (define (a2 quad) (cadr quad))

Evaluating a floating point variable in Scheme language

落爺英雄遲暮 提交于 2019-12-02 08:14:15
问题 I want to read multiple data files (10 in total) in Ansys Fluent. I wrote a journal file which uses scheme language (Do ((count 11.100 (+ count 0.100))) ((>= count 12.000)) (ti-menu-load-string (format #f "file read-data data-~a.dat" count))) The format of the file name is like data-11.200.dat , but the program reads it as data-11.2.dat . How I can force it to read the floating point numbers after the decimal point? Of course I can rename the data files, but that is not useful for I have to

Running queries against a list of lists in Scheme

跟風遠走 提交于 2019-12-02 08:06:17
I'm stuck in the middle of my project. I have a list of lists like: '((a for apple) (b is book) (c in cat) (ronn live in NY)) Now I want to make a query in the form of a list and have it display the correct entry in my list of lists. For example, if I input '(a for what) or '(what in cat) it will display (a for apple) or (c in cat) . If I input '(ronn live in where) it will show (ronn live in NY) . Can anyone help me solve this problem? How about running a filter routine across the list, and using a lambda object initialized with your query information that will then be applied to the list