racket

Update the whole structure

十年热恋 提交于 2019-12-24 10:48:13
问题 Suppose I have some function which returns a struct: (struct layer (points lines areas)) (define (build-new-layer height) ... (layer list-a list-b list-c)) I want to keep track of the last returned result something like: (define top-side (build-new-layer 0)) ; store the first result ... (set! top-side (build-new-layer 0.5)) ; throw away the first result and store the new one However, for that particular code I get the error: set!: assignment disallowed; cannot modify a constant constant: top

Return a pair - syntax error

。_饼干妹妹 提交于 2019-12-24 10:47:33
问题 I'm using pl in racket: https://pl.barzilay.org/ The download can be found here: http://pl.barzilay.org/pl.plt ( : f1 : -> (Pairof Symbol String)) (define (f1) (cons 'wwww "aaa")) Error: Type Checker: Polymorphic function `cons' could not be applied to arguments: Argument 1: Expected: a Given: 'wwww Argument 2: Expected: (Listof a) Given: String Result type: (Listof a) Expected result: (Pairof Symbol String) in: (cons (quote wwww) "aaa") What I did wrong and how can I fix it? 回答1: The #lang

Visualize arbitrary tree in Racket using tree-layout

喜夏-厌秋 提交于 2019-12-24 10:02:11
问题 How to visualise arbitrary tree? for example: (define T1 '(and (or x1 x2)(or x3 x4 x5))) or one generated with: (define functions '(not if and or)) (define terminals '(A0 A1 A2 D0 D1)) (define functions&terminals (append terminals functions )) (define (pick-one list) (list-ref list (random (length list)))) (define arities '((if . 3)(and . 2)(or . 2)(not . 1))) (define (terminal? symbol) (find (lambda (x)(eq? x symbol)) terminals)) (define (function? symbol) (find (lambda (x)(eq? x symbol))

for loop in scheme

我只是一个虾纸丫 提交于 2019-12-24 08:34:26
问题 i'm kinda of confused how i can construct a for loop in scheme. the for-loop should be implemented in Part II. where it takes a list of numbers and insert each element inside the list in Part I to find the length. I was cable to get the first element but i need a for loop or what so ever to get an output like this: '(7 10 5 16 106 37) here is my code : #lang racket ; Part I (define (sequence n) (cond [(= n 1) (list n)] [(even? n) ( cons n(sequence( / n 2)))] [(odd? n) ( cons n(sequence (+(* n

Case Statement Not Assigning Value

一笑奈何 提交于 2019-12-24 08:18:52
问题 I'm having some trouble debugging a case statement. I was hoping that the statement would assign numeric values to note-val , but so far it is assigning #<void> . I know it's something wrong with the case statement, because if I add an else clause, that value gets applied. Given a sample input of '(((#\3 #\A) (#\4 #\B)) ((#\4 #\C))) , what am I doing wrong here? (In regards to the case statement. I'm sure there are other errors, but I'd like to try to work those out myself if I can get this

Racket, include, require and provide don't work

微笑、不失礼 提交于 2019-12-24 07:39:45
问题 I have a file with the name “functions.rkt”, where I have some functions. And I am working in another file, let’s name it “working.rkt” I have tried the following (one by one) at “working.rkt” to use the function defined at “functions.rkt”: (require “functions.rkt”) (include “functions.rkt”) (provide “functions.rkt”) And anyone of them hasn’t worked, any help? They are in the same path. 回答1: In the file "functions.rkt: #lang racket (provide my-function) (define (my-function x) (* 2 x)) In the

Reordering parentheses using associative property in Racket

别来无恙 提交于 2019-12-24 06:37:16
问题 I am having trouble implementing a function that given something like this: '((+(d + e) + f) + (a + (a + c)) Returns this: '(d + (e + (f + (a + (a + c))))) Now the catch is that I have to use associativity to get the answer so I only want to use this property: '((a + b) + c) = '(a + (b + c)) To get the correct output. I know how to implement the function for this case: '((a + b) + c) -> '(a + (b + c)) However I cannot seem to figure out how to implement it for the first case above (or any

Compare two lists and return false if they are not equal scheme

被刻印的时光 ゝ 提交于 2019-12-24 03:39:27
问题 i would like to ask you for help to complete code below with condition which is testing if lists ws and vs are not equal. If they are not equal so return text false(#f) else process code below. I stared with fulfilling variables len1 and len2 which are counting length of both lists. When i run it i am getting this error: lambda: no expression after a sequence of internal definitions in: lambda What i am doing wrong? (define (weighted-sum . ws) (define (sub . vs) (let ((len1 (length ws)) (len2

using stop-when in racket

泪湿孤枕 提交于 2019-12-24 03:35:12
问题 I've been messing around with this program. It takes a number and adds 1 to it. I am wondering how exactly could you use stop-when here? For example, to make it stop at 5? I suppose a cond statement is necessary here. Thanks. (require 2htdp/image) (require 2htdp/universe) (define (my-tick n) (add1 n)) (define (my-render n) (text (number->string n) 36 "silver")) (big-bang 1 (on-tick my-tick 2) (to-draw my-render)) 回答1: Give stop-when a predicate that consumes a world and returns true or false.

Intersect more lists in Scheme

て烟熏妆下的殇ゞ 提交于 2019-12-24 02:14:14
问题 I am trying to intersect more lists in Scheme and I need a little help. The lists look like this: The first two: (((?x john) (?city new-york)) ((?x mike) (?city chicago)) ((?x mary) (?city london))) and (((?city chicago)) ((?city new-york))) I need to look in every list (say A) from the first list and see if there is a list (say B) in the second one so that A has at least one element in common with B. If there is no such element, the resulting list will not contain A. The result for the two