scheme

Cons box implementation

爷,独闯天下 提交于 2019-12-12 06:45:35
问题 I cannot seem to figure out a way to do Scheme's cons boxes in Ruby (it seems its pretty much all arrays). This is a pretty rough outline: class cons def initialize (car, cdr) @car = car @cdr = cdr end #return the car of the pair def car return @car end #return the cdr of the pair def cdr return @cdr end end I can pass two values and call the car and cdr , but this is not a list of any sort (just two values). How do I make a list on which I can insert something as in Scheme cons: myCons =

How to make eval work on define?

风格不统一 提交于 2019-12-12 05:49:18
问题 I posted some other easy code, to clarify what's happening When I use eval on the following code #lang racket (define (test ) `( (define num 1) (define l (list)) (define num2 (add1 num)) (displayln num2))) (eval (test) (make-base-namespace)) racket screams at me define-values: not in a definition context in: (define-values (num) 1) My questions are: How to make eval work on definition? If eval is not designed to work on definitions, then is there some workarounds that can make it work? I

tree-equal function not giving the correct output

怎甘沉沦 提交于 2019-12-12 05:31:24
问题 I am writing code to test if two trees are equal (in data and structure) in Scheme, and I have to assume that I only have at most two children for each node. My code is as follows: (define (make-tree value left right) (list value left right)) (define (value tree) (car tree)) (define (left tree) (car (cdr tree))) (define (right tree) (car (cdr (cdr tree)))) (define (tree-equal? T1 T2) (if (and (null? T1) (null? T2)) #t (if (= (value T1) (value T2)) (tree-equal? (left T1) (left T2)) (tree-equal

Scheme rewrite let* as nested unary lets

China☆狼群 提交于 2019-12-12 05:29:01
问题 I have written a function match-rewriter that is essentially match-lambda except that it returns its argument if no match is found: (define-syntax match-rewriter (syntax-rules () ((_ (patt body) ...) (λ (x) (match x (patt body) ... (_ x)))))) Now I would like to use match-rewriter to take strings representing source code for let* and rewrite it as nested unary lets : (define let*→nested-unary-lets (match-rewriter (`(let*((,<var> ,<val>) ...) ,<expr1> ,<expr2> ...) I am really stumped over how

Scheme: creating a random range [closed]

谁说胖子不能爱 提交于 2019-12-12 05:08:11
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . In scheme I have to use random to define a procedure that accepts no arguments and returns an integer in the range 1 to 10, inclusive and i cant use if. im lost =( 回答1: If your Scheme provides a random function,

how do you append list of lists without using append in scheme?

痴心易碎 提交于 2019-12-12 04:51:51
问题 Say you want append these list of lists and output a single list that contains all the numbers (append-lists (list (list 1 2) (list 4 5) (list 10 19))) => (list 1 2 4 5 10 19) If using trivial append, i can do this, ((define (append-lists llon) (cond [(empty? llon) empty] [(cons? llon) (cons (first llon) (append-lists (rest llon)))])) But how to get the same output without using append just by recursion? 回答1: This is a special case of flattening . Some Scheme implementation feature a build-in

Writing a scheme function

戏子无情 提交于 2019-12-12 04:43:39
问题 How do I write a function that takes both a scoring function (which I've written already) and a list of pairs of strings as input (which I'm confused on how to write), and returns a modified list of pairs of strings, where the returned list should contain all the optimal string pairs from the input, scored according to the input function. Example input: '( ("hello" "b_low") ("hello_" "b_l_ow") ("hello" "_blow") ("hello" "blow") ("h_e_llo" "bl_o__w") ) Example output: ( ("hello" "b_low") (

higher order procedure to print alternating pictures

江枫思渺然 提交于 2019-12-12 04:35:46
问题 definition of task : i have to make pumpkins and fishes hanging on a string terms used : what-is-it? ==>a function that determines whether to make a fish or a pumpkin fish-squared ==> a function to make a fish using 2 parameters pumpkin ==> a function to make a pumpkin with also 2 parameters decorations ==> a function that appends all the images together hang-by-thread ==> a function that hangs all the images to a thread extra for this exercise i have to use" (if (odd? k) fish-square pumpkin)

scheme structures and lists

匆匆过客 提交于 2019-12-12 04:35:22
问题 (define-struct position (name numshares share-price)) (define p1 (cons (make-position "INT" 10 192) (cons (make-position "SSS" 4 42) empty))) mult is my helper function (define (mult n) ( * (position-numshares n) (position-share-price n))) const takes the position-numshares and the position-share-price in a list and multiplies them together. (define (const n) (cond [(empty? n) empty] [(cons? n) (+ (mult (first n)) )])) What I would like to do is take the first of the list and add the rest of

scheme how do you sum numbers in a list when you have structures and list of lists

流过昼夜 提交于 2019-12-12 04:34:10
问题 ;; An ATOM is one of: ;; -- Symbol ;; -- String ;; -- Number ;; An SEXP (S-expression) is one of: ;; -- empty ;; -- (cons ATOM SEXP) ;; -- (cons SEXP SEXP) So i'm trying to sum up all the numbers in SEXP! Here's my code, ;; sum-numbers: sexp -> Number (define (sum-numbers sexp) (cond [(empty? sexp) 0] [(ATOM? (first sexp)) (+ (atom-sum-numbers (first sexp)) (sum-numbers (rest sexp)))] [(SEXP? (first sexp)) (+ (sum-numbers (first sexp)) (sum-numbers (rest sexp)))])) ;; atom-sum-numbers: Atom -