racket

DrRacket procedure body help (boolean-odd? x)

落花浮王杯 提交于 2019-12-13 10:09:59
问题 An iterative version of odd? for non-negative integer arguments can be written using and, or, and not. To do so, you have to take advantage of the fact that and and or are special forms that evaluate their arguments in order from left to right, exiting as soon as the value is determined. Write (boolean-odd? x) without using if or cond, but using and, or, not (boolean) instead. You may use + and -, but do not use quotient, remainder, /, etc. 回答1: A number is even if two divides it evenly, and

how to do multiply-all function in RACKET

做~自己de王妃 提交于 2019-12-13 09:48:27
问题 Exercise 22.5.11 Develop a function multiply-all that takes in a list of numbers and returns the result of multiplying them all together. For example: (check-expect (multiply-all (cons 3 (cons 5 (cons 4 empty)))) 60) Hint: What is the “right answer” for the empty list? It may not be what you think at first! Solution: The data definition is similar to that for list-of-strings: ; A list-of-numbers is either ; empty or ; a nelon (non-empty list of numbers). #| (define (function-on-lon L) ; L a

IN Racket Define a function that takes two arguments

落爺英雄遲暮 提交于 2019-12-13 08:57:33
问题 I need some one can explain for me to how to do this please Define a function that takes two arguments, a list of numbers and a single number (the threshold). It should return a new list that has the same numbers as the input list, but with all elements greater than the threshold number removed. You may not use the built-in filter function as a helper function. Your implementation must be recursive. INPUT: A list of numbers and a single atomic number. OUTPUT: A new list of numbers that

Homework: Sublist? checking if an item is a sublist of the first one

大憨熊 提交于 2019-12-13 07:57:56
问题 So I have this program that needs to be written in Scheme using Racket that has the following properties and I am stumped. The function is called sublist? with two inputs of S and L which are both lists. It checks whether S is a sublist of L and returns #t or #f . Examples would be similar to: sublist? of (A A) and (A B C) is #f sublist? of (A B C) and (A B D A B C D) is #t sublist? of (A (B)) and (C ((A (B))) (C)) is #t A small function called extractLists needs to be created to extract the

Count atoms in a list structure

混江龙づ霸主 提交于 2019-12-13 07:45:38
问题 I need to find how many elements a given input has. the input can be a list or a symbol, for example: 'a => 1 element '(3 . 4) => 2 elements '(a b . c) => 3 elements '((a b . c) 3 . 4) => 5 elements one problem is that when I'm going through the input, every element can be a list of its own, or a pair (just started learning scheme, so my tools for right now are mainly car/cdr), so, when should I stop my loop? when if (null? x) condition is true? or maybe when if (null? (car x)) is true? 回答1:

In Racket, is it possible to have multiple event-handlers in big-bang?

送分小仙女□ 提交于 2019-12-13 05:41:21
问题 I am wanting to do two different things at each tick. Is it possible to have multiple on-tick event handlers in a big-bang environment? This is what I would like to do: (big-bang world (on-draw show-world) (on-tick event1 event2 1)) or (big-bang world (on-draw show-world) (on-tick event1 1) (on-tick event2 1)) Neither of these methods is allowed. Is there a way to do this? Thank you. 回答1: What would it mean? For example, suppose that ;; A World is a Nat (a natural number). and here are the

Can syntax parameters be used to replace syntax?

妖精的绣舞 提交于 2019-12-13 05:26:41
问题 I'm trying to use syntax parameters in order to inject new syntax where I need it to be injected. The result of this is then used in other syntax. However, it's not working as I expect it to. Here's a minimal working example: (require racket/stxparam) (require (for-syntax racket/stxparam)) ;; declare parameter to be replaced with code (define-syntax-parameter placeholder (lambda (stx) (raise-syntax-error (syntax-e stx) "can only be used inside declare-many-commands"))) ;; this is just to

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

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