scheme

Scheme Derivative Function

拥有回忆 提交于 2019-12-02 08:05:55
问题 I wrote a simple derivative function in scheme today. I was asked to return a function such that g(x) = (f (x+h) -f(x))/h . Does this suffice to return a function or does this only return a value? (define (der f h) (lambda (x) (/ (- (f(+ x h)) (f x)) h))) 回答1: Yes, the code in the question is returning a function (that's what the lambda is for). If it were returning a value , it'd be missing the line with (lambda (x) and the corresponding closing parentheses. Also notice that although the

a function median in Scheme

落爺英雄遲暮 提交于 2019-12-02 07:59:28
I am new to Scheme and I am using Dr.Racket to try to find the median of the list. If the length of list L is odd, the function median returns the median element in the list. If the length of L is even, the function median returns 0. example (median ‘(1)) returns 1 (median ‘(1 2)) returns 0 (median ‘(1 2 3)) returns 2 (median ‘( 1 2 3 4) returns 0 i am only allowed to use - null? - car - cdr - else - = - + - median - cond - if - user defined names (for my variables) - integer literals - parentheses Any ideas? This problem can be solved using the tortoise and hare algorithm , provided that a

multiplicative inverse of modulo m in scheme

我与影子孤独终老i 提交于 2019-12-02 07:37:43
问题 I've written the code for multiplicative inverse of modulo m. It works for most of the initial cases but not for some. The code is below: (define (inverse x m) (let loop ((x (modulo x m)) (a 1)) (cond ((zero? x) #f) ((= x 1) a) (else (let ((q (- (quotient m x)))) (loop (+ m (* q x)) (modulo (* q a) m))))))) For example it gives correct values for (inverse 5 11) -> 9 (inverse 9 11) -> 5 (inverse 7 11 ) - > 8 (inverse 8 12) -> #f but when i give (inverse 5 12) it produces #f while it should

How to replace an item by another in a list in DrScheme when given paramters are two items and a list?

╄→гoц情女王★ 提交于 2019-12-02 07:11:33
问题 How to replace an item by another in a list in DrScheme when given paramters are two items and a list? 回答1: Use map with a function which returns the replacement item when its argument is equal to the item you want to replace and the argument otherwise. 回答2: ; replace first occurrence of b with a in list ls, q? is used for comparison (define (replace q? a b ls) (cond ((null? ls) '()) ((q? (car ls) b) (cons a (cdr ls))) (else (cons (car ls) (replace a b (cdr ls)))))) 回答3: ; replace first

How to construct a tree of particular shape with elements from a list

旧巷老猫 提交于 2019-12-02 06:51:20
问题 Given an s-expression '((a . b) . (c . d)) and a list '(e f g h) , how can I traverse the s-expression create an s-expression with the same shape, but with elements taken from the list? E.g., for the s-expression and list above, the result would be '((e . f) g . h) ? 回答1: Traversing a tree of pairs in left to right order isn't particularly difficult, as car and cdr let you get to both sides, and cons can put things back together. The tricky part in a problem like this is that to "replace"

Searching and replacing n element on list - scheme

匆匆过客 提交于 2019-12-02 06:40:30
问题 have got a problem with do this kind of code , can't figure how to search for a element (a) and replace i by ( b) , how to do it? Thx in advance 回答1: Try this function: (define subst (lambda (new old l) (cond ((null? l) (quote ())) ((atom? (car l)) (cond ((eq? (car l) old) (cons new (subst new old (cdr l)))) (else (cons (car l) (subst new old (cdr l)))))) (else (cons (subst new old (car l)) (subst new old (cdr l))))))) This will search through a list of S expressions and substitute every

A function which will determine that if a passed in list follows an A B pattern

时光总嘲笑我的痴心妄想 提交于 2019-12-02 05:57:17
(define fun4 (lambda ( ls) (cond ((null? ls ) #f) (cons (((eqv? 'a (car ls))) && ((eqv? 'b (cdr ls))))) (else (pattern2 cdr ls))))) In this it showing error - procedure application: expected procedure, given: #t (no arguments), What is the erroe in my code. Is logic is fine ??? There are many, many errors in your solution. Let's see what's wrong in each of the conditions: The base case of the recursion (empty list) is wrong: an empty list is the exit of the recursion, and it means that the list was traversed correctly and it follows the pattern Another base case is missing: what if the list

Upside down text

夙愿已清 提交于 2019-12-02 05:49:22
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. 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 . #\ɟ) (#\g . #\ƃ) (#\h . #\ɥ) (#\i . #\ı) (#\j . #\ɾ) (#\k . #\ʞ) (#\l . #\ן) (#\m . #\ɯ) (#\n . #\u) (#\o . #\o) (#

Scheme - find most deeply values nested lists

北城以北 提交于 2019-12-02 05:43:34
I asked several days ago about finding the most deeply nested lists . I implemented the idea that was given, and it works. But there is another problem: I also need to build a list from the nested list. meaning: If I change (8) and (10 11 12) , to leaf1 and leaf2, I need to return: '(ans (leaf1 (8)) (leaf2 (10 11 12)) . /ans is a quote In other words: my function will get (1 (2 3) (4 (5) (7 (8) (10 11 12)))))) => the most nested lists are (8) and (10 11 12) => my function will return '(ans (leaf1 (8)) (leaf2 (10 11 12)) . I am trying to find an idea, not an implementation . Thanks. Yes, this

Custom map function - how does it work?

半城伤御伤魂 提交于 2019-12-02 05:40:19
I apologize for the unclear topic title. I have this function in Scheme which is a custom implementation of the map function. It works fine, but I got lost trying to understand it. (define (my-map proc . ls) (letrec ((iter (lambda (proc ls0) (if (null? ls0) '() (cons (proc (car ls0)) (iter proc (cdr ls0)))))) (map-rec (lambda (proc ls0) (if (memq '() ls0) '() (cons (apply proc (iter car ls0)) (map-rec proc (iter cdr ls0))))))) (map-rec proc ls))) The problem lays in cons (proc (car ls0)) . If I'm correct, when passing (1 2 3) (4 5 6) to the ls parameter the actual value of it will be ((1 2 3)