scheme

What is the “return” in scheme?

*爱你&永不变心* 提交于 2019-12-11 01:57:55
问题 I am trying to translate some python code to scheme. def test(x): if x > 1: do something if x > 10: return if x == 4: do something test(11) I did not find the "return" in scheme. I want to quit from the test function when x >10. How can I simulate the "return" in scheme? (I am using guile) Thanks 回答1: In Scheme there isn't an explicit return keyword - it's a lot simpler than that, the value of the last expression in a sequence of expressions is the one that gets returned. For example, your

SCHEME recursion perfect number (beginner, hopefully easy fix)

北城余情 提交于 2019-12-11 01:49:04
问题 having an issue with my perfect number function. The objective of the code is to determine if the number is a perfect number, meaning it is equal to the sum of its divisors. Ex:6. Im having trouble with my code. Here's my function: (define (is-perfect x) (define (divides a b) (= (modulo b a) 0)) (define (sum-proper-divisors y) (if (= y 1) 1 (if (divides y x) (+ y (sum-proper-divisors (- y 1))) (if (= x 1) #f (= (sum-proper-divisors (- x 1) x))))))) 回答1: You almost got it! there are a couple

Scheme - How to use “.” as a symbol

萝らか妹 提交于 2019-12-11 00:51:43
问题 I want to do something like: (car '(. a)) and get . as a result. For example, if you type '. into the console you will get the output that I want. The problem is that I don't want to have an apostrophe infront of all of the . in a list. Any guidance? 回答1: In Scheme's read syntax, a standalone dot is special. '. won't get you a dot symbol; it's invalid syntax. (If it works in your implementation, then that's just a special quirk of your implementation.) Instead, you have to escape it. In most

functions and lists in scheme/racket

人盡茶涼 提交于 2019-12-11 00:43:54
问题 How would you define a function which takes one argument, which should be a list, and returns the elements in the list which are themselves lists? (check-expect (find-sublists ’(1 2 () (3) (a b c) a b c)) ’(() (3) (a b c))) 回答1: Do you have experience designing functions that can filter through a list? A simpler problem with the same flavor as the original is something like this: design a function that takes a list of numbers and keeps only the even numbers. Would you be able to do that

Union of set of ranges in Scheme

北城以北 提交于 2019-12-10 23:57:18
问题 I need to write a Scheme function (union s1 s2) that when given two sets, let's say s1 = ((1 3) (5 13) (25 100)) s2 = ((2 4) (17 26) (97 100)) will give (union s1 s2) ----> ((1 4) (5 13) (17 100)) Also if s1 = ((1 3) (5 13) (25 110) (199 300)) s2 = ((2 4) (17 26) (97 100) (110 200) (288 500)) then (union s1 s2) ----> ((1 4) (5 13) (17 500)) Can anybody suggest me how to do this? I have no idea how to start. 回答1: Instead of working with 2 sets, I would suggest merging the 2 sets into one

add1 function from scheme to R5RS

自古美人都是妖i 提交于 2019-12-10 23:25:13
问题 I've written some code but it's not working because the add1 function I used in Scheme is not working with R5RS. What can replace add1 in R5RS? 回答1: The procedure add1 is very simple, you can implement it yourself: (define (add1 x) (+ x 1)) 回答2: late answer but an alternative is (making use of lambdas) .. (define add1 (lambda (x) (+ x 1))) 来源: https://stackoverflow.com/questions/13292094/add1-function-from-scheme-to-r5rs

Detecting the caller of a function in Scheme or Racket

拈花ヽ惹草 提交于 2019-12-10 22:36:19
问题 In Scheme or Racket is it possible to detect the caller of a function? For example, I can write a function to test if a list is a list of atoms as follows: (define atom? (lambda (x) (and (not (pair? x)) (not (empty? x))))) (define lat? (lambda (l) (define latt? (lambda (l) (cond ((null? l) #t) ((atom? (car l)) (latt? (cdr l))) (else #f)))) (if (null? l) #f (latt? l)))) but instead of the above, is there a function like "called-by" to do something like this: (define lat? (lambda (l) (cond (

Scheme - Optional arguments and default values

不想你离开。 提交于 2019-12-10 21:10:09
问题 I'm currently looking into Scheme, and the way I have understood it, procedures can take an arbitrary number of arguments. I have been trying to play around with this, but I'm struggling to grasp the concept. For instance, say I want to write a welcome message, based on information provided by the user. If user provides a first and last name, the program shout write: Welcome, <FIRST> <LAST>! ;; <FIRST> = "Julius", <LAST>= "Caesar" Welcome, Julius Caesar! Otherwise, the program should refer to

define a form as function name?

江枫思渺然 提交于 2019-12-10 20:38:13
问题 I'd like to know what this code means in Scheme: (define ((K x) y) x) (define (((S x) y) z) ((x z) (y z))) The whole file is here. Is this legal Scheme? Is (K x) a parametrized function, something like generic functions in Java? I looked up the MIT Scheme reference, there seems to be nothing mentioned for definition of this kind. 回答1: Trying it in MIT Scheme works (define ((K x) y) x) ;Value: k ((k 3) 4) ;Value: 3 Apparently, these are the definitions for K and S combinators from a

Recursive Function Composition in Scheme

泪湿孤枕 提交于 2019-12-10 20:02:06
问题 Below is an attempt I've made to create a procedure that returns the function composition given a list of functions in scheme. I've reached an impasse; What I've written tried makes sense on paper but I don't see where I am going wrong, can anyone give some tips? ; (compose-all-rec fs) -> procedure ; fs: listof procedure ; return the function composition of all functions in fs: ; if fs = (f0 f1 ... fN), the result is f0(f1(...(fN(x))...)) ; implement this procedure recursively (define compose