racket

How to print a string in backward, in scheme?

不羁的心 提交于 2019-12-22 23:11:23
问题 I know if I write my scheme code in the following way and type in (word ‘(a b c)), it will out put the list in the same order. Could you please tell me if there was a way I can print it out in opposite order. Ex- (list ‘c ‘b ‘a). it needs to be the user's input I print out in opposite order. So, I can't call it (reverse '(a b c)). since the user input can be something like '(x y z). Thanks a lot. (define(word x ) (if(null? x) x (cons(car x)(word (cdr x))))) (word '(a b c)) (list 'a 'b 'c) 回答1

How to create a list with n applications of a procedure

末鹿安然 提交于 2019-12-22 18:51:13
问题 My question is related to this one but in my case I would like to obtain a list with the results of n applications of a function whose output is not computable again with the previous result (picking randomly an element from a list, for example). That is, it is not the composition of a function n times with itself but the n results shown together into a list. 回答1: Something like this? #!racket/base (require srfi/1) (define (times/list proc n) (unfold-right zero? proc sub1 n)) (times/list

How to create a list with n applications of a procedure

时光总嘲笑我的痴心妄想 提交于 2019-12-22 18:51:11
问题 My question is related to this one but in my case I would like to obtain a list with the results of n applications of a function whose output is not computable again with the previous result (picking randomly an element from a list, for example). That is, it is not the composition of a function n times with itself but the n results shown together into a list. 回答1: Something like this? #!racket/base (require srfi/1) (define (times/list proc n) (unfold-right zero? proc sub1 n)) (times/list

Binary trees in scheme

三世轮回 提交于 2019-12-22 18:46:10
问题 How would I count how many nodes a tree has? ;;; A Binary is one of: ;;; - Number ;;; - (make-node BT BT) (define-struct node (left right)) (define tree1 (make-node (make-node 10 9) (make-node 3 (make-node 1 5)))) (define (how-many? nd) (cond [(number? nd)....] [(node? n) (..... (how-many? (node-left nd)) (how-many? (node-right nd)))])) so for tree1 I should get (check-expect (how-many? tree1) 5) I think I got the template right. If it's a number, you should return 1 . But if it's a node ,

Macro to use dot.notation to get structure fields in Racket

纵然是瞬间 提交于 2019-12-22 10:53:19
问题 For a structure and its instance defined as follows: (struct dog (name breed age)) (define mydog (dog "lassie" "collie" 5)) (example from https://learnxinyminutes.com/docs/racket/) The usual way to get a structure field is as follows: (dog-name mydog) ; => "lassie" Can there be a macro so that we can perform above using following dot notation: (mydog.name) ; => "lassie" Or, parentheses may not be needed: mydog.name ; => "lassie" Macro for dot notation is used on this page: http://www

Racket: Identifying tail recursion?

自古美人都是妖i 提交于 2019-12-22 07:18:08
问题 I wrote two different functions in racket to determine whether a list of numbers is ascending: (define (ascending list) (if (<= (length list) 1) #t (and (< (car list) (car (cdr list))) (ascending (cdr list))))) (define (ascending-tail list) (ascending-tail-helper #t list)) (define (ascending-tail-helper prevBool rest) (if (<= (length rest) 1) prevBool (ascending-tail-helper (and prevBool (< (car rest) (car (cdr rest)))) (cdr rest)))) I had the hardest time determining whether or not the first

Drawing onto canvas% element

我是研究僧i 提交于 2019-12-22 06:56:01
问题 I have a problem while trying to draw onto a canvas GUI element. I create a frame, a canvas and try to draw on the dc context of the canvas with the draw-line method, but nothing happens. The frame with the canvas is shown, but the line isn't shown on the canvas. (require racket/gui/base) (define frame (new frame% [label "Frame"] [width 500] [height 500])) (define canvas (new canvas% [parent frame])) (define dc (send canvas get-dc)) (send dc draw-line 10 10 200 200) (send frame show #t) Does

scheme string-append? recursion to replicate a string

浪尽此生 提交于 2019-12-21 21:25:24
问题 Design a program called string-dup that consumes a String s and a Number n and returns a String that is the concatenation of s n times with spaces between each instance of s, i.e., (string-dup "a" 3) => "a a a" Without using replicate but i guess we can use string-append. So far i got (define (string-dup s n) (cond [(zero? n) ""] [else (cond [(= n 1 ) s] [(> n 1 )(string-dup (string-append s " ") (sub1 n))])])) However, this only allows me to have one string "a". I know when in the list case

How to split list into evenly sized chunks in Racket (Scheme)?

穿精又带淫゛_ 提交于 2019-12-21 17:35:13
问题 Example: How to convert list: '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) Into list of lists: '((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15)) Based on answers provided here so far, this is what I've come up with: First define function to take up to 'n' elements from beginning of the list: (define (take-up-to n xs) (define (iter xs n taken) (cond [(or (zero? n) (empty? xs)) (reverse taken)] [else (iter (cdr xs) (- n 1) (cons (car xs) taken))])) (iter xs n '())) Second is similar function for the

How to eval strings in racket

旧时模样 提交于 2019-12-21 09:14:30
问题 I'm trying to understand how to get the eval function to read a string and evaluate the content that's inside the string. Currently I know that > (eval '(+ 1 2)) 3 but I'm not that knowledgeable with the use of racket. So at the moment I'm trying to get it so that I can do this: > (eval "(+ 1 2)") 3 Any advice or links to useful resources would be appreciated. 回答1: You want to use read together with open-input-string . Like so: -> (eval (read (open-input-string "(+ 1 2)"))) 3 You can also use