scheme

Searching and replacing n element on list - scheme

坚强是说给别人听的谎言 提交于 2019-12-02 05:38:25
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 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 occurrence of old with an occurrence of new . Start with a list. If it's empty, leave it. If the first element

MIT Scheme Message Passing Abstraction

元气小坏坏 提交于 2019-12-02 05:33:27
In a Computer Science course I am taking, for homework, we were tasked with several different questions all pertaining to message passing. I have been able to solve all but one, which asks for the following: Write a mailman object factory (make-mailman) that takes in no parameters and returns a message-passing object that responds to the following messages: 'add-to-route : return a procedure that takes in an arbitrary number of mailbox objects and adds them to the mailman object's “route” 'collect-letters : return a procedure that takes in an arbitrary number of letter objects and collects

Evaluating a floating point variable in Scheme language

浪子不回头ぞ 提交于 2019-12-02 05:32:46
I want to read multiple data files (10 in total) in Ansys Fluent. I wrote a journal file which uses scheme language (Do ((count 11.100 (+ count 0.100))) ((>= count 12.000)) (ti-menu-load-string (format #f "file read-data data-~a.dat" count))) The format of the file name is like data-11.200.dat , but the program reads it as data-11.2.dat . How I can force it to read the floating point numbers after the decimal point? Of course I can rename the data files, but that is not useful for I have to use the code many times. I have tried data-~03d.dat , but that didn't work! Try this: (do ((count 111/10

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

本小妞迷上赌 提交于 2019-12-02 05:18:18
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) ? 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" elements in the right hand side of a tree, you need to know how many of the available inputs you used when

Convert from procedure form to let form

佐手、 提交于 2019-12-02 05:15:04
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) (let ((PI 3.14159265)) (let ((areac (lambda(d) (*PI(/ d 2)(/ d 2)) (* (/ h 3))))))))) (define TotalVolume

Scheme Error Object Is Not Applicable

。_饼干妹妹 提交于 2019-12-02 05:00:50
I am writing a Scheme function that detects if a word is in a list of words. My code uses an if statement and memq to return either #t or #f. However, something is causing the first parameter to return the error that the object is not applicable. (define in? (lambda (y xs) ((if (memq( y xs )) #t #f)))) Parentheses matter: (define in? (lambda (y xs) (if (memq y xs) #t #f))) so you have double parentheses before if you put memq parameters between parentheses BTW, you can also express this as (define in? (lambda (y xs) (and (memq y xs) #t))) 来源: https://stackoverflow.com/questions/26287081/scheme

Different nCurses behaviours with different terminals

限于喜欢 提交于 2019-12-02 04:33:51
问题 I obtain two different behaviours using different terminals, this is my code: (use ncurses) (initscr) (curs_set 0) (noecho) (start_color) (define win (newwin 20 50 1 1)) (wclear win) (box win 0 0) (for-each (lambda (y) (for-each (lambda (x) (mvwaddch win y x #\. )) (iota 49))) (iota 19)) (wrefresh win) (wgetch win) (endwin) The code is written in Chicken Scheme but it's easily readable by anyone who knows nCurses. I think my problem doesn't concern the library because it's a simple wrapper

Scheme Derivative Function

对着背影说爱祢 提交于 2019-12-02 04:26:14
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))) 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 procedure is correct, the formula stated in the question isn't right, it should be: g(x) = (f(x+h) - f(x))/h ;

Turn string into number in Racket

大憨熊 提交于 2019-12-02 04:15:33
问题 I used read to get a line from a file. The documentation said read returns any , so is it turning the line to a string? I have problems turning the string "1" to the number 1 , or "500.8232" into 500.8232 . I am also wondering if Racket can directly read numbers in from a file. 回答1: Check out their documentation search, it's complete and accurate. Conversion functions usually have the form of foo->bar (which you can assume takes a foo and returns a bar constructed from it). You sound like you

getting the largest number in a list in scheme

断了今生、忘了曾经 提交于 2019-12-02 03:59:39
I do not understand why my function to get the largest number does not want to work. If I am thinking about this correctly, if the first atom is smaller than the second atom then you call the function minus the first one in the list, else you construct the first atom, largest one, with the rest of the list. relevant code: (define (getlargest a_list) (cond ((null? a_list) '()) ((< (car a_list) (cadr a_list)) (getlargest (cdr a_list))) (else (cons (car a_list) (getlargest(cdr a_list)))))) Your current procedure is failing at runtime. Even if it didn't, you're comparing one element with the next,