scheme

Why we can define a function with the same name of a built-in function Racket?

十年热恋 提交于 2019-12-12 01:36:28
问题 We can define a new function like this: (define (car x y) (+ x y)) And use car as an add function. Meanwhile, we lost the built-in function car . Why does Racket allow this? How could we recover the lost built-in function, here is car . 回答1: Definitions affect the current module only (and, if you export your definition, then any other modules that import your module). You can always import Racket's built-in functions under a different name, if you want to use car in your module for something

(Scheme) Take a list and return new list with count of positive, negative, and zeros

我是研究僧i 提交于 2019-12-11 23:25:31
问题 I am attempting to accept a list, have it count the positive, negative, and zeros, and return a new list. The only thing I notice as I'm debugging is that the list is iterating through, but it is not utilizing any of the conditionals. So its successfully recursively calling itself, and then it just errors once its empty. (define (mydisplay value) (display value) (newline) #t ) (define neg 0) (define z 0) (define pos 0) (define (posneg lst) (cond ((NULL? lst)) (NEGATIVE? (car lst) (+ 1 neg))

Understanding a tail-recursive vector

醉酒当歌 提交于 2019-12-11 23:24:11
问题 I have a tail-recursive function which converts a vector into a list. I understand each line individually, but have a couple questions: Firstly, in the code what does the code cons ((vector-ref v i) r) (- i 1) mean? (Marked "Q1".) I know that it takes the i 'th element of vector v and concatenates it with i-1 , but why have to be i-1 ? Why not work with i+1 ? e.g. if the vector v length is total 5, then element number 5 is concatenated with the number 4. I understand that it is making the

Multiplying Elements in a List Containing Pairs

好久不见. 提交于 2019-12-11 22:40:00
问题 I'm trying to write a code (function) using Scheme that: Takes a list of any size as a parameter Multiplies every number of the list together Symbols should be skipped over Values inside pairs should be included in multiplication In other words, results should be as follows: > (mult '(1 2 3)) 6 > (mult '(1 2 x 3 4)) 24 > (mult '(1 2 z (3 y 4))) 24 (mine gives me 2) My code allows me to skip over the symbols and multiply everything. However, once I include a pair inside the list, it acts as

Scheme Recursion Loop Incorrect Values and Variable Binding

六月ゝ 毕业季﹏ 提交于 2019-12-11 20:34:09
问题 I started a question here about a hangman game I am working on. Recursive Scheme Function Value Error I feel that the hangman portion is confusing people from my real issue and question. My problem is, I call various defined functions from within my recursion loop and get incorrect values. When I call these same function by themselves (not in a recursive loop) they work as expected. I know it is either something I am overlooking or a variable binding issue is going on that I need a workaround

“Multiplication of Arbitrary Precision Numbers” in Scheme

走远了吗. 提交于 2019-12-11 20:28:34
问题 The following is code to a problem that I have been working on for a few days. The problem I encountered is that for some reason when I call: (apa-multi '(7 3 1 2) '(6 1 4)) the return is: '(4 8 9 5 6 8) The answer that it should output is '(4 4 8 9 5 6 8) When I call: (apa-multi '(3 1 2) '(6 1 4)) The output is: '(1 9 1 5 6 8) which is correct. I have debugged my code multiple times, and I can't seem to find out what the problem is (by the way, I know that the "remove-empty" function that I

Scheme how to define var in if condition

岁酱吖の 提交于 2019-12-11 20:25:31
问题 I am newbie to Scheme programming and trying to define a var in if condition. For example, I have: (if (< x y) (define x y) ) ;(GOAL: if x < y, than x=y..) But I got the error: let: bad syntax (not an identifier and expression for a binding) in:... Any ideas how to resolve this, would be greatly appreciated. p.s. Sorry for my English 回答1: Using define is wrong; you are not defining a function here. There are two solutions: (if (< x y) (set! x y) (void)) ; if x < y set x = y; else do nothing

Scheme - Function recursion error

天涯浪子 提交于 2019-12-11 20:01:09
问题 This function reads in a list and swaps values, but only when there's a key in the hash table that matches an element in the list. However, the list that is read in could contain other lists, and I want to check this recursively. I use if (list? elementInList) to determine whether I'm dealing with a list, so I can search that list for elements that may need to be swapped using a recursive call. I tried to do this but it's not handling the lists within the list correctly. What am I doing wrong

Remove duplication from the Scheme function

南楼画角 提交于 2019-12-11 18:37:45
问题 (define (merge-sorted lst1 lst2) (cond ((null? lst1) lst2) ((null? lst2) lst1) ((>= (car lst1) (car lst2)) (cons (car lst2) (merge-sorted lst1 (cdr lst2)))) (else (cons (car lst1) (merge-sorted (cdr lst1) lst2))))) Output: (merge-sorted '(1 3 4) '(2 4 5)) => '(1 2 3 4 4 5) I have to write function on lists in Scheme. How can I fix the duplication? 回答1: Instead of having >= as one condition, you can test equality separately, whereby whenever (car lst1) is equal to (car lst2) , you would keep