racket

create sqrt function in racket

被刻印的时光 ゝ 提交于 2019-12-13 22:47:43
问题 I tried to create a sqrt+ function, which will get a list of numbers and return a list of numbers. Can anyone tell me what's wrong with the function? #lang pl 03 (: sqrt+ : (Listof Number) -> (Listof Number)) ;; a version of `sqrt' that takes a list of numbers, and return a list ;; with twice the elements, holding the two roots of each of the inputs; ;; throws an error if any input is negative. (define (sqrt+ ns) (cond [(null? ns) 0] [(< (first ns) 0) (error 'ns "`sqrt' requires a nonnegative

Sorting in Ascending Order in Scheme Programming [closed]

筅森魡賤 提交于 2019-12-13 22:26:10
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I would like to sort some data and I would like to use symbol 回答1: I believe Racket's sort can do what you want, with the appropriate less-than and #:key parameter. For example: > (sort '((strawberry 5 zambia) (banana 2 sweden)) symbol<? #:key first) => '((banana 2 sweden) (strawberry 5 zambia))

Combine two lists into one list Racket

我们两清 提交于 2019-12-13 22:19:19
问题 I have a question: I have two list of numbers for example (list1 3 6 7) and (list2 1 6 4 7). Now I have to combine both into (list3 1 3 4). So 6 and 7 are both in list1 and list2. List3 contains all numbers which occur only once. I hope u get what i mean if not just ask me :s! Here my start: (define (diff list1 list2) (cond [(empty? list1) list2] ;; If list1 was empty return directly list2 [(empty? list2) list1] ;; If list2 was empty return directly list1 [else (??? I know that I have to

Scheme: how to produce '(5 . (5))

旧巷老猫 提交于 2019-12-13 20:12:42
问题 I have tried all kinds of combinations of cons and append to produce '(5 . (5)) but I couldn't. Is there any way? 回答1: At the risk of sounding like Bill Clinton, it depends on what you mean by "produce". If you mean "produce a value that prints on the screen as '(5 . (5)) , then you're sort of out of luck, because this value prints as '(5 5) . For a similar example: how do I produce the number 1e-1 ? Well, try typing it in; this is the same as 0.1, and if you type in 1e-1, it's going to print

Scheme - Replacing elements in a list with its index

青春壹個敷衍的年華 提交于 2019-12-13 19:30:59
问题 I am trying to replace the elements in a scheme list with its position. For example, calling: (position '((a b) c)) should return: '((0 1) 2) So far, my code keeps the list format, but the index is not updating. (define (position term1) (define index 0) (cond [(null? term1) '()] [(list? term1) (cons (position (car term1)) (position(cdr term1)))] [else (+ 1 index) index])) When (position '((a b) c)) is called, it returns '((0 0) 0) Can anybody explain why the index isn't updating? 回答1: There

(SCHEME) Number -> English List

依然范特西╮ 提交于 2019-12-13 19:28:08
问题 Okay. So I'm wondering how to create a function that will turn a random number into its english word component. Such as (1001 -> '(one thousand one) or 0 -> '(zero) and (number-name factorial 20 -> ’(two quintillion four hundred thirty two quadrillion nine hundred two trillion eight billion one hundred seventy six million six hundred forty thousand)) I worked with a previous user on stackoverflow to get something that turned a long number into 3 part digits (1,341,100 is one million, 341

gen:custom-write for Racket classes

丶灬走出姿态 提交于 2019-12-13 16:13:58
问题 I am looking for the canonical way to specify custom methods to output the fields of a Racket object. In other words I'm looking for the Racket equivalent of Java's toString method (if it exists). I know that for structs one can use gen:custom-write to specify the write-proc function (source). Is there something similar for classes? 回答1: Yes for custom-write . Since gen:custom-write is a wrapper around prop:custom-write, it is possible to have a class implement it through an interface. The

Any idea of how to interleave two lists in dr racket?

淺唱寂寞╮ 提交于 2019-12-13 14:07:49
问题 The problem is when lists have a different length, any idea of how to do it? I have to use functions like map or something like that This is the code I wrote so far, it works with lists of the same length but it also needs to work with lists of different lengths. Thank you. (define (interleave list1 list2) (flatten [map (lambda (x y) (cons x (cons y null))) list1 list2])) if lists have different length this is what I get: map: all lists must have same size; arguments were: # '(1 2 3 4 5) '(a

how to spell a number in racket? (spellNum) [closed]

十年热恋 提交于 2019-12-13 11:16:39
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am new to Racket and I want to write a function spellNum which does this: (spellNum 467) ---> '(four six seven) I don't know how to start. I have started programming by seeing the online documentation, but I don't know how to declare a variable in Dr Racket. I am basically a Python guy. can

DrRacket - why is this number negative?

早过忘川 提交于 2019-12-13 11:03:16
问题 So I can not figure out why my numbers are negative in this function. Also, the input for calculate is supposed to be a list of the same 3 if someone could give me a hand with that as well, it would be much appreciated. Thank you. calculate takes the first number in the list, then multiplies it by the second number in the list and subtracts the third number from the input list. ((calculate '(8 3 7)) '(4 8 2 9)) should return '(29 41 23 44) (define (calculateHelper n m o L) (if (null? L) empty