racket

How write a function that returns a list of 'n' functions based on an input

a 夏天 提交于 2019-12-02 10:56:02
I'm looking to create a function that returns a list of 'n' functions each of which increments the input by 1, 2, 3... n respectively. I use DrRacket to try this out. A sample of expected outcome : > (map (lambda (f) (f 20)) (func-list 5)) (21 22 23 24 25) I'm able to write this down in a static-way : > (define (func-list num) > (list (lambda (x) (+ x 1)) (lambda (x) (+ x 2)) (lambda (x) (+ x 3)) (lambda (x) (+ x 4)) (lambda (x) (+ x 5))) [Edit] Also that a few restrictions are placed on implementation : Only 'cons' and arithmetic operations can be used The func-list should take as input only

“application: not a procedure” while computing binomial

ⅰ亾dé卋堺 提交于 2019-12-02 10:06:43
I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error: application: not a procedure; expected a procedure that can be applied to arguments given: 1 arguments...: 2 I don't understand the error because I thought this defined my function: (define (binomial n k) (cond ((or (= n 0) (= n k)) 1) (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) In Scheme (and Lisps in general), parentheses are placed before a procedure application and after the final argument to the procedure. You've done this correctly in, e.g., (= n 0) (= n k) (- k 1) (binomial(- n 1) (-

Scheme changing tree values

大憨熊 提交于 2019-12-02 09:44:04
I need to implement a procedure called inverse-tree that receives a tree whose nodes data values are numbers and booleans and returns the equivalent tree whose nodes satisfy the following: If the equivalent node of the original tree is a number, then the resulting tree’s node is −1· that node value If the equivalent node of the original tree is a boolean, then the resulting tree’s node is the logical not of that node value Examples: > (inverse-tree ’()) ’() > (inverse-tree ’(5)) ’(-5) > (inverse-tree ’(0)) ’(0) > (inverse-tree ’(#f)) ’(#t) > (inverse-tree ’(#t)) ’(#f) > (inverse-tree ’(-5 (1 (

Converting numbers to english letter list

谁都会走 提交于 2019-12-02 09:42:53
I have the function below which converts an input of numbers into the partially translated word output of those numbers. Using product and quotient, it adds the word representation of numbers while splitting the number into groups. For example: (number-name 87969087) -> '(87 million 969 thousand 87) (number-name 1000000) -> '(1 million) Im trying to complete my problem by fully translating those numbers which are less than 1000 as well. Im trying to implement a function less-than-1000 which will display those smaller numbers as the list is being constructed as well. Alongside: ;; for less than

Transform a natural number to a specific base and return it as a list

此生再无相见时 提交于 2019-12-02 09:07:56
I want to show the result of my function as a list not as a number. My result is: (define lst (list )) (define (num->base n b) (if (zero? n) (append lst (list 0)) (append lst (list (+ (* 10 (num->base (quotient n b) b)) (modulo n b)))))) The next error appears: expected: number? given: '(0) argument position: 2nd other arguments...: 10 I think you have to rethink this problem. Appending results to a global variable is definitely not the way to go, let's try a different approach via tail recursion: (define (num->base n b) (let loop ((n n) (acc '())) (if (< n b) (cons n acc) (loop (quotient n b)

Reverse list in Racket in O(n)

本小妞迷上赌 提交于 2019-12-02 09:04:19
I need to write a recursive function in Scheme which takes a list of atoms and reverses it in linear time. I am only allowed to use define, lambda, cons, car, cdr, cond, let, and null? . Here is what I have so far: (define reverse (lambda (lat) (cond ((null? lat) lat) (else (cons (reverse (cdr lat)) (cons (car lat) '())))))) So when I call the function: (reverse '(a b c d)) I get the following output: '(() (((() 4) 3) 2) 1) Any help would be very much appreciated. The problem is that if (reverse (cdr lat)) returns a list eg (3 2) then (cons '(3 2) (1)) turns into ((3 2) 1) . A classical way to

In Racket, if an unquoted pair is constructed with the dot notation, is it possible to use a variable or an expression value for the second element?

≯℡__Kan透↙ 提交于 2019-12-02 08:53:23
问题 In Racket, the following works: (+ . [1 2]) ; => 3 { define a + } (a . [1 2]) ; => 3 However, i see no way to define b to be the (1 2) list so as to get (+ . b) and (a . b) to return 3 . Is it possible? 回答1: Sure, just use apply : (define a +) (define b '(1 2)) (apply a b) ; => 3 (apply + b) ; => 3 回答2: How about this ... without using apply but using eval . But seriously, using apply is a better idea in this case, there's nothing wrong with it ( eval is evil though, see the documentation to

DrRacket - Display all the values in a list that are above average

浪尽此生 提交于 2019-12-02 08:40:26
I'm creating a function that consumes a list of numbers and produces the elements in the list that are above average. Below is my code: (define (listlength list) (cond ((empty? list) 0) (else (+ 1 (listlength (rest list)))))) (define (listsum list) (cond [(empty? list) 0] [else (+ (first list) (listsum (rest list)))])) (define (average log) (/ (listsum log) (+ (listlength log) 1))) (define (average-filter log) (cons (cond [(> (first log) (average log)) (first log)] [else (average-filter (rest log))]))) So obviously there is something wrong with my code...Can someone help me? The error message

How to replace an item by another in a list in DrScheme when given paramters are two items and a list?

╄→гoц情女王★ 提交于 2019-12-02 07:11:33
问题 How to replace an item by another in a list in DrScheme when given paramters are two items and a list? 回答1: Use map with a function which returns the replacement item when its argument is equal to the item you want to replace and the argument otherwise. 回答2: ; replace first occurrence of b with a in list ls, q? is used for comparison (define (replace q? a b ls) (cond ((null? ls) '()) ((q? (car ls) b) (cons a (cdr ls))) (else (cons (car ls) (replace a b (cdr ls)))))) 回答3: ; replace first

Trying to get this code to work, can't understand where to put the argument in and keep getting errors

♀尐吖头ヾ 提交于 2019-12-02 06:02:14
Define the function iota1(n, m) that takes positive integers n, m with n < m as input, and outputs the list (n,n+1,n+2,...,m) I've tried switching the code around multiple times but cannot seem to get it to function and display a list the right way (define (iota1 n m) (if (eq? n 0) '() (append (iota1 (< n m) (+ n 1)) (list n)))) There's a few oddities to the code you provided, which I've formatted for readability: (define (iota1 n m) (if (eq? n 0) '() (append (iota (< n m) (+ n 1)) (list n)))) The first is that the expression (< n m) evaluates to a boolean value, depending on whether n is less