scheme

Upside down text

╄→гoц情女王★ 提交于 2019-12-02 12:53:13
问题 How would you design a program that will take in a string of lower case letters and produce the string upside down? so if I type in home i get ǝɯoɥ upside down. I've tried looking for in the book to get started, but nothing. 回答1: Try this, a bit of a brute-force approach but works quite well for uppercase, lowercase and number characters - all other characters are presented just as they come: (define upside-map '#hash( (#\a . #\ɐ) (#\b . #\q) (#\c . #\ɔ) (#\d . #\p) (#\e . #\ǝ) (#\f . #\ɟ) (#

Turning structural recursion into accumulative recursion in Racket

∥☆過路亽.° 提交于 2019-12-02 12:20:22
I have some code to find the maximum height and replace it with the associated name. There are separate lists for height and names, each the same length and non-empty. I can solve this using structural recursion but have to change it into accumulative, and I am unsure how to do that. All the examples I have seen are confusing me. Is anybody able to turn the code into one using accumulative recursion? (define (tallest names heights) (cond [(empty? names) heights] [(> (first heights) (first (rest heights))) (cons (first names) (tallest (rest (rest names)) (rest (rest heights))))] [else (tallest

How can I increment a value in scheme with closure?

夙愿已清 提交于 2019-12-02 12:19:11
How can I increment a value in scheme with closure? I'm on lecture 3A in the sicp course. (define (sum VAL) // how do I increment VAL everytime i call it? (lambda(x) (* x x VAL))) (define a (sum 5)) (a 3) Use set! for storing the incremented value. Try this: (define (sum VAL) (lambda (x) (set! VAL (add1 VAL)) (* x x VAL))) Because VAL was enclosed at the time the sum procedure was called, each time you call a it'll "remember" the previous value in VAL and it'll get incremented by one unit. For example: (define a (sum 5)) ; VAL = 5 (a 3) ; VAL = 6 => 54 ; (* 3 3 6) (a 3) ; VAL = 7 => 63 ; (* 3

Convert from procedure form to let form

有些话、适合烂在心里 提交于 2019-12-02 11:45:17
问题 have this procedure form code that I have written in scheme and I need to change it into let form. Here is procedure form code: (define PI 3.14159265) (define areac (lambda (d) (* PI (/ d 2) (/ d 2)))) (define volumec (lambda (d h) (*(areac d)(/ h 3)))) (define TotalVolume (lambda() (+(volumec 1 1) (volumec 2 2) (volumec 3 3) (volumec 4 4) (volumec 5 5)))) (define main (lambda() (TotalVolume))) (main) Here is what I have implemented so far for my let form code: (define volumec (lambda(d h)

Scheme run length encoding

不问归期 提交于 2019-12-02 11:19:42
The problem is to: Write a function (encode L) that takes a list of atoms L and run-length encodes the list such that the output is a list of pairs of the form (value length) where the first element is a value and the second is the number of times that value occurs in the list being encoded. For example: (encode '(1 1 2 4 4 8 8 8)) ---> '((1 2) (2 1) (4 2) (8 3)) This is the code I have so far: (define (encode lst) (cond ((null? lst) '()) (else ((append (list (car lst) (count lst 1)) (encode (cdr lst))))))) (define (count lst n) (cond ((null? lst) n) ((equal? (car lst) (car (cdr lst))) (count

Using car and cdr

笑着哭i 提交于 2019-12-02 11:07:50
I am new to scheme and having a hard time with using car and cdr. I have an AST string literal in ast. (define ast '(program ((assign (var i int) (call (func getint void int) ())) (assign (var j int) (call (func getint void int) ())) (while (neq (var i int) (var j int)) ((if (gt (var i int) (var j int)) ((assign (var i int) (minus (var i int) (var j int)))) ((assign (var j int) (minus (var j int) (var i int))))))) (call (func putint int void) ((var i int))))) ) I know car returns the head of ast. So (car ast) returns 'program. I am confused how to use car and cdr to get strings from ast such

How write a function that returns a list of 'n' functions based on an input

a 夏天 提交于 2019-12-02 10:56:02
I'm looking to create a function that returns a list of 'n' functions each of which increments the input by 1, 2, 3... n respectively. I use DrRacket to try this out. A sample of expected outcome : > (map (lambda (f) (f 20)) (func-list 5)) (21 22 23 24 25) I'm able to write this down in a static-way : > (define (func-list num) > (list (lambda (x) (+ x 1)) (lambda (x) (+ x 2)) (lambda (x) (+ x 3)) (lambda (x) (+ x 4)) (lambda (x) (+ x 5))) [Edit] Also that a few restrictions are placed on implementation : Only 'cons' and arithmetic operations can be used The func-list should take as input only

Find an element of list in lists in list

放肆的年华 提交于 2019-12-02 10:21:39
I need a procedure which takes a list and checks if an element is part of that list even when the list contains lists. So far, I've written this: (define (element-of-set? element set) (cond ((null? set) #f) ((eq? element (car set)) #t) (else (element-of-set? element (cdr set)))))) But, if you have a list that looks like this: (a (a b b (c b) 3) 5 5 (e s) (s e s)) then my written element-of-set? does not recognize that 3 is a part of this list. What am I doing wrong? You're not recurring down the car of your set argument. If you evaluate (element-of-set? 3 '(a (a b b (c b) 3) 5 5 (e s) (s e s))

How to determine if a list has an even or odd number of atoms

十年热恋 提交于 2019-12-02 10:15:10
tScheme novice question: I need to determine if a list contains an even or odd amount of atoms using recursion. I know the easy way is to get list length and determine if it is even or odd, but I would like to see hows it's done using recursion. (oddatom (LIST 'x 'y 'v 'd 'r 'h 'y)) should return #t , while (oddatom '((n m) (f p) l (u k p))) should return #f appreciate the help. Here's my version of the solution: (define (oddatom? lst) (let recur ((odd #f) (x lst)) (cond ((null? x) odd) ((pair? x) (recur (recur odd (car x)) (cdr x))) (else (not odd))))) Joshua Taylor I like Chris Jester-Young

“application: not a procedure” while computing binomial

ⅰ亾dé卋堺 提交于 2019-12-02 10:06:43
I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error: application: not a procedure; expected a procedure that can be applied to arguments given: 1 arguments...: 2 I don't understand the error because I thought this defined my function: (define (binomial n k) (cond ((or (= n 0) (= n k)) 1) (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) In Scheme (and Lisps in general), parentheses are placed before a procedure application and after the final argument to the procedure. You've done this correctly in, e.g., (= n 0) (= n k) (- k 1) (binomial(- n 1) (-