scheme

how to do multiply-all function in RACKET

做~自己de王妃 提交于 2019-12-13 09:48:27
问题 Exercise 22.5.11 Develop a function multiply-all that takes in a list of numbers and returns the result of multiplying them all together. For example: (check-expect (multiply-all (cons 3 (cons 5 (cons 4 empty)))) 60) Hint: What is the “right answer” for the empty list? It may not be what you think at first! Solution: The data definition is similar to that for list-of-strings: ; A list-of-numbers is either ; empty or ; a nelon (non-empty list of numbers). #| (define (function-on-lon L) ; L a

determine whether undirected graph has path between two vertices

£可爱£侵袭症+ 提交于 2019-12-13 09:13:53
问题 I need a function which determines whether a path exists between vertices. Input: undirected graph as a list two vertices For example: (is_it_a_path? '(2 ((1 2) (3 4))) 1 4) ;; returns true The function also needs to be tail recursive. How do I do this? 回答1: The (free, online) textbook How To Design Programs has several sections that may be helpful to you. You say that the solution must be tail-recursive. If you mean that all calls to the search procedure must be in tail position, then you're

Problems with recursion trying to learn Scheme

若如初见. 提交于 2019-12-13 08:26:44
问题 I have created a function that will insert a list into a defined emtpy list. The list that I am inserting will have some type of keyword for finding it (for example maybe a name or a location) followed by a nested list that will contain some sort of information such as an age of a call record or something (again this is just me trying to learn the recursive syntax). My problem lies in how do I traverse through the bigger list and let the program know that there are several lists within a

Homework: Sublist? checking if an item is a sublist of the first one

大憨熊 提交于 2019-12-13 07:57:56
问题 So I have this program that needs to be written in Scheme using Racket that has the following properties and I am stumped. The function is called sublist? with two inputs of S and L which are both lists. It checks whether S is a sublist of L and returns #t or #f . Examples would be similar to: sublist? of (A A) and (A B C) is #f sublist? of (A B C) and (A B D A B C D) is #t sublist? of (A (B)) and (C ((A (B))) (C)) is #t A small function called extractLists needs to be created to extract the

Count atoms in a list structure

混江龙づ霸主 提交于 2019-12-13 07:45:38
问题 I need to find how many elements a given input has. the input can be a list or a symbol, for example: 'a => 1 element '(3 . 4) => 2 elements '(a b . c) => 3 elements '((a b . c) 3 . 4) => 5 elements one problem is that when I'm going through the input, every element can be a list of its own, or a pair (just started learning scheme, so my tools for right now are mainly car/cdr), so, when should I stop my loop? when if (null? x) condition is true? or maybe when if (null? (car x)) is true? 回答1:

Way of returning median value of a list? (scheme)

帅比萌擦擦* 提交于 2019-12-13 06:12:07
问题 I'm attempting to make a procedure named median that takes the median value of a list. If the list is even, then I will return the two middle numbers. I have the logic all thought out in my head, but I'm not sure how to complete it. NOTE: I am trying to avoid using list-ref, as it would trivialize the problem. So far, my code looks like the following. (define (median lst) (if (null? lst) '() (if (even? lst) ; ends here Now, my approach to the problem is this. Odd #- Return the value of the

Scheme - using apply

℡╲_俬逩灬. 提交于 2019-12-13 05:25:24
问题 I received the following exercise as homework. I sat on it for hours without any success, so I have no choice but to use your help. Examples: (define m1 (cons "fixNumber" (lambda () 42))) (define m3 (cons "add" (lambda (x y) (+ x y)))) (define myobj (create-obj (list m1 m2 m3))) (myobj "fixNumber" '()) ;; => 42 (myobj "add" '(1 2)) ;; => 3 (myobj "myMethod" '()) ;; => "Error: no such method" 回答1: This should do: (define (create-obj mlist) (lambda (method parms) (let ((func (assoc method mlist

Issues with conditionals in Scheme

大兔子大兔子 提交于 2019-12-13 05:05:51
问题 I'm using Scheme with the full Swindle package, and I'm trying to use conditionals to recursively determine evenness/oddity of integers. My code is as follows: (define (odd? x)( (cond ((= x 0) '#f) ((= x 1) '#t) ((= (even? (- x 1)) #t) '#f) (else '#t)))) (define (even? x)( (cond ((= x 0) '#f) ((= x 2) '#t) ((= (odd? (- x 1)) #t) '#f) (else '#t)))) However, when I run (even? x) or (odd? x) [x is some number, doesn't matter what, as I get the same error] I get: application: not a procedure;

How to know if a binary tree is ordered scheme

左心房为你撑大大i 提交于 2019-12-13 04:59:29
问题 A binary tree is ordered if all its children are under the left branch that the data of the root and branch of the right age and, in turn, binary trees of the left and right branches are also sorted. Write the procedure (ordered-btree? btree) receiving a binary tree as argument and returns True if ordered and false if not. How can I do this? 回答1: There are several ways to solve this problem. I'll assume that there are no repeated elements in the tree and that you have written helper

Error in conditional (cond …) script-fu

我与影子孤独终老i 提交于 2019-12-13 04:48:37
问题 I'm trying to do a script-fu and I'm using a cond statement theoretically correct, but it always gives the error "Error: ( : 1) illegal function ". This is the code: (define (script-fu-prueba edicionInteractiva) (let* ( (cond ( (equal? edicionInteractiva "Interactivo") (edicionInteractiva RUN-INTERACTIVE) ) ( (equal? edicionInteractiva "No interactivo") (edicionInteractiva RUN-NONINTERACTIVE) ) ) ) ) ) (script-fu-register "script-fu-prueba" "<Image>/Filters/PRUEBA" "Prueba" "Author"