racket

(Scheme) Take a list and return new list with count of positive, negative, and zeros

我是研究僧i 提交于 2019-12-11 23:25:31
问题 I am attempting to accept a list, have it count the positive, negative, and zeros, and return a new list. The only thing I notice as I'm debugging is that the list is iterating through, but it is not utilizing any of the conditionals. So its successfully recursively calling itself, and then it just errors once its empty. (define (mydisplay value) (display value) (newline) #t ) (define neg 0) (define z 0) (define pos 0) (define (posneg lst) (cond ((NULL? lst)) (NEGATIVE? (car lst) (+ 1 neg))

Scheme Recursion Loop Incorrect Values and Variable Binding

六月ゝ 毕业季﹏ 提交于 2019-12-11 20:34:09
问题 I started a question here about a hangman game I am working on. Recursive Scheme Function Value Error I feel that the hangman portion is confusing people from my real issue and question. My problem is, I call various defined functions from within my recursion loop and get incorrect values. When I call these same function by themselves (not in a recursive loop) they work as expected. I know it is either something I am overlooking or a variable binding issue is going on that I need a workaround

Scheme how to define var in if condition

岁酱吖の 提交于 2019-12-11 20:25:31
问题 I am newbie to Scheme programming and trying to define a var in if condition. For example, I have: (if (< x y) (define x y) ) ;(GOAL: if x < y, than x=y..) But I got the error: let: bad syntax (not an identifier and expression for a binding) in:... Any ideas how to resolve this, would be greatly appreciated. p.s. Sorry for my English 回答1: Using define is wrong; you are not defining a function here. There are two solutions: (if (< x y) (set! x y) (void)) ; if x < y set x = y; else do nothing

Remove duplication from the Scheme function

南楼画角 提交于 2019-12-11 18:37:45
问题 (define (merge-sorted lst1 lst2) (cond ((null? lst1) lst2) ((null? lst2) lst1) ((>= (car lst1) (car lst2)) (cons (car lst2) (merge-sorted lst1 (cdr lst2)))) (else (cons (car lst1) (merge-sorted (cdr lst1) lst2))))) Output: (merge-sorted '(1 3 4) '(2 4 5)) => '(1 2 3 4 4 5) I have to write function on lists in Scheme. How can I fix the duplication? 回答1: Instead of having >= as one condition, you can test equality separately, whereby whenever (car lst1) is equal to (car lst2) , you would keep

Sorting a hash table in Racket

心已入冬 提交于 2019-12-11 17:36:29
问题 I'm new to Racket and I'm trying to define a function sort-mail that is gonna sort a hash table. I've some defined lists: (define test-dates '("Sun, 10 Sep 2017 09:48:44 +0200" "Wed, 13 Sep 2017 17:51:05 +0000" "Sun, 10 Sep 2017 13:16:19 +0200" "Tue, 17 Nov 2009 18:21:38 -0500" "Wed, 13 Sep 2017 10:40:47 -0700" "Thu, 14 Sep 2017 12:03:35 -0700" "Wed, 18 Nov 2009 02:22:12 -0800" "Sat, 09 Sep 2017 13:40:18 -0700" "Tue, 26 Oct 2010 15:11:06 +0200" "Tue, 17 Nov 2009 18:04:31 -0800" "Mon, 17 Oct

Finding all possible lists of X

落花浮王杯 提交于 2019-12-11 16:27:50
问题 I would like to create a function construct-tuples that consumes a list of length m and a Nat n and produces all possible n-tuples of a list of the elements of the consumed list. The following check-expects give an idea of what the function should produce: (check-expect (construct-tuples '(+ -) 3) '((+ + +) (+ + -) (+ - +) (+ - -) (- + +) (- + -) (- - +) (- - -))) (check-expect (construct-tuples empty 3) (list empty)) (check-expect (construct-tuples '(+ -) 0) (list empty)) (check-expect

Dr. Racket Recursion count occurrences

瘦欲@ 提交于 2019-12-11 16:20:02
问题 I'm new to Racket and trying to learn it. I'm working through some problems that I'm struggling with. Here is what the problem is asking: Write a definition for the recursive function occur that takes a data expression a and a list s and returns the number of times that the data expression a appears in the list s. Example: (occur '() '(1 () 2 () () 3)) =>3 (occur 1 '(1 2 1 ((3 1)) 4 1)) => 3 (note that it only looks at whole elements in the list) (occur '((2)) '(1 ((2)) 3)) => 1 This is what

Having Trouble Returning the Deepest Pair of the List

試著忘記壹切 提交于 2019-12-11 15:51:50
问题 #lang racket (define (depth L) (cond ((null? L) 0) ;if list is null return 0, base case ((not(list? L)) 0) ;if list is just an atom also return 0, also a base case (else (max (+ 1 (depth(car L))) (depth(cdr L)))))) ;otherwise return the greater of recursive call (depth(car list)) +1 or (depth(cdr list)) ;the logic here using findMax is that to see which one of the recursion is deeper, and everytime car is called we add 1 to the depth I tried to implement a scheme to find the maximum depth of

(2d) perlin-noise: dot product of a gradient and a direction vector

*爱你&永不变心* 提交于 2019-12-11 15:04:17
问题 I am looking for a function that takes the coordinates of a current pixel and the coordinates of a corner (for a gradient), and returns the dot product of the gradient of that corner and the direction vector between the pixel and a corner. So the function would look like this: dot-grid-gradient: number number number number -> number 64 grids and 81 gradients: I already made a list of 81 gradients whose x and y coordinates were between 0 and 1 and all randomly generated. A gradient would be

Record error in Scheme/Racket - why do you need to define constructors as mutable?

江枫思渺然 提交于 2019-12-11 14:49:43
问题 i'm having trouble defining some code in scheme. I am trying to create a record for a node in Scheme/Racket, so far my code looks as follows: (define-record-type node (make-node v l r) node? (v tree-value) (l tree-left) (r tree-right)) However - when I try and execute I get the following error: define-record-type: expected a mutable', immutable', parent', protocol', sealed', opaque', nongenerative', or parent-rtd' clause in: (make-node v l r) I understand that you can define field types to be