scheme

Scheme mode function

狂风中的少年 提交于 2020-01-06 13:10:50
问题 I am trying to find the mode of a list Assuming the list is sorted ascending order Here is my mode function (define freq (lambda (l) (cond ((null? l)l) ((null? (cdr l))l) ((not(equal? (car l) (car(cdr l))))(freq(cdr(delete l (car l))))) (else (freq (cdr l))) ))) (freq '(4 4 4 4 5 7 9 9 9)) => should returns 4 but its returning 9 instead 回答1: Here's my solution, which is similar to Óscar's solution but centralises the update of the longest/winning result in one place: (define (longest-run lst)

How to generate list of prime pairs that can be concatenated to new prime?

徘徊边缘 提交于 2020-01-06 06:59:31
问题 I have a list of primes stored in a variable primes-list-split that are represented in a list format by a number->list method. The format is as follows: the prime list of 2, 3, 5, 7, and 11 would be '((2) (3) (5) (7) (1 1)) . get-prime-pairs takes as input this list, and tries to find every combination that concatenates to another prime. Then it should return these pairs in a list in the list representation. So the pair 3 367 which concatenates to 3367 which is also prime should be in the

Scheme Inserting Pairs into Heap

孤人 提交于 2020-01-06 05:51:57
问题 This is part of a homework assignment that I can't seem to figure out. I was wondering if someone could point me in the right direction? The problem reads: (insert-list-of-pairs vw-pair-list heap) evaluates to the heap resulting from inserting all of the value - weight pairs from the list vw-pair-list into the heap heap . The following functions were created earlier in the homework for use in this problem: (define (create-heap vw-pair left right) (list vw-pair left right)) (define (h-min heap

Scheme - define variable as the result of a function?

风流意气都作罢 提交于 2020-01-06 05:23:13
问题 The beginning of one of my programs results in an error. This is the problem area. I am trying to define a variable as the result of a recursive function. (define (test n) (define (a1func i) (if (= i 1) 0 (+ (/ 1 i) (a1func (- i 1))))) (define a1 (a1func (- n 1)))) if you were to give it say (test 10) the error would be: procedure application: expected procedure, given: #<undefined> ; arguments were: 9 I assumed this could be done in Scheme?? ideas? 回答1: In pure FP languages computations are

Longest common sublist

故事扮演 提交于 2020-01-06 02:47:06
问题 While trying to write a solution to the longest common sublist problem in Scheme, I'm having trouble figuring out what is wrong with what I have so far. I think it's the right idea and before worrying about polynomial time I'm just trying to get one that works at all. I haven't written in a functional language before and the syntactic differences can make things a little harder at first. (define (lcs lst1 lst2) (if (or (null? lst1) (null? lst2)) '() (if (not (null? lcs)) lcs (if (equal? (car

Converting a list of digits to a number

纵饮孤独 提交于 2020-01-05 12:17:25
问题 I was wondering if there was a way to take a list of numbers (digits), and truncate the numbers together to be one large number (not addition) in Scheme. For example, I would want (foo '(1 2 3 4)) ;=> 1234 Does Scheme have a built in function to do this? 回答1: There are a number of languages that are in the Scheme family, and there are a few versions of Scheme, too. If you're using one, e.g., Racket, that includes a left associative fold (often called foldl , fold , or reduce , though there

Converting a list of digits to a number

冷暖自知 提交于 2020-01-05 12:17:09
问题 I was wondering if there was a way to take a list of numbers (digits), and truncate the numbers together to be one large number (not addition) in Scheme. For example, I would want (foo '(1 2 3 4)) ;=> 1234 Does Scheme have a built in function to do this? 回答1: There are a number of languages that are in the Scheme family, and there are a few versions of Scheme, too. If you're using one, e.g., Racket, that includes a left associative fold (often called foldl , fold , or reduce , though there

Scheme / Racket Vector in Vector transformation

元气小坏坏 提交于 2020-01-05 10:27:21
问题 I'm having a problem transforming a vector like this: #(#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3))) Into one like this: #(#(1 1 1 1 1) #(2 2 2 2 2) #(3 3 3 3 3)) I wrote a piece of test code but the output is wrong. I went into the debugger and I think I know which line of code cause the problem. I can't seems to find a way to make it work. Any help is greatly appreciated. (define (test) (let* ((table #(#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3)#(1 2 3))) (counter 5) (size 3) (new-table (make-vector

Representing an amount of money with specific bills

笑着哭i 提交于 2020-01-05 07:59:53
问题 I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type to make the given amount in total. For example (calc 415 (list 100 10 5 2 1)) should return '(4 1 1 0 0) . I tried it this way but this doesn't work :/ I think I haven't fully understood what you can / can't do with set! in Racket, to be honest. (define (calc n xs) (cond ((null? xs) (list)) ((not (pair? xs)) (define y n)

Scheme accumulative recursion with lists

╄→гoц情女王★ 提交于 2020-01-05 06:01:19
问题 How can I pass a list as a parameter to a function adding elements to it recursively,and have it unmodified when it comes out of recursion? I want to use the list at each level of recursion with the list having the values added by deeper recursion levels. To be more specific I want to do a DFS search on a graph and I want to store in the list the nodes I visited. 回答1: One method of doing this is just to return the list so you have access to it at higher levels of recursion. Another method is