scheme

Why (apply and '(1 2 3)) doesn't work while (and 1 2 3) works in R5RS? [duplicate]

微笑、不失礼 提交于 2019-12-28 07:02:08
问题 This question already has answers here : Using AND with the apply function in Scheme (9 answers) Closed 6 years ago . I tried it in Racket like this > (apply and '(1 2 3)) . and: bad syntax in: and > (and 1 2 3) 3 Does anyone have ideas about this? 回答1: Chris Jester-Young's answer is right, but there's one other point I want to highlight. The standard and operator is a macro which delays the evaluation of its arguments, by (essentially, if not exactly) turning (and a b c) into (if a (if b c

Flatten a list using only the forms in “The Little Schemer”

谁说我不能喝 提交于 2019-12-27 12:27:12
问题 I'm going through The LIttle Schemer to learn Scheme (as an old C programmer) and as an exercise I tried to write a procedure to flatten a list using only the forms in The Little Schemer; I.e., define , lambda , cond , car , cdr , and , or , etc., but not append . I thought it would be easy but I haven't been able to come up with a solution. How can I do this ? 回答1: I have a version that uses only "first-principles" operations and is efficient (does not require more than one pass through any

Scheme: Counting Structures

和自甴很熟 提交于 2019-12-26 15:06:50
问题 (count-by-type (list (make-animal "Slytherin" "snake" (make-date 2013 8 23)) (make-animal "Toby" "dog" (make-date 2014 3 20)) (make-animal "Curly" "dog" (make-date 2014 1 18)) (make-animal "Maximus" "cat" (make-date 2013 10 7)) (make-animal "Mia" "cat" (make-date 2013 10 7)))) => (list (list 2 "cat") (list 2 "dog") (list 1 "snake")) ;; A Date is a structure (make-date y m d), where ;; y is positive integer (year), ;; m is an integer between 1 and 12 (month), ;; d is an integer between 1 and

Scheme functions and lists, multiply,add

别来无恙 提交于 2019-12-25 20:05:06
问题 i am trying to write 2 functions in scheme, the first would multiply every value in a list by a user specified value, 2nd function would add a number to all the values in the list from previous result. i tried something like that but racket throws an error. (define test (list 1 1 2 3 5)) (define funca(*(test)(2))) 回答1: In Scheme we use the map higher-order procedure for applying a function over a list of elements - bear in mind that you can't multiply a list, what we can do is multiply each

“application: not a procedure” in determinant code

拟墨画扇 提交于 2019-12-25 18:38:25
问题 application: not a procedure; expected a procedure that can be applied to arguments given: 2 arguments...: -4 -6 12 -3 1 2 7 What does this error mean given the following code? (define (det2x2 a b c d)(- (* a d) (* b c))) (define (det2x2prod a1 b1 c1 d1 a2 b2 c2 d2) (det2x2 (+ (* a1 a2)(* b1 c2)) (+ (* a1 b2)(* b1 d2)) (+ (* c1 a2)(* d1 c2)) (+ (* c1 b2) (* d1 d2)))) (det2x2prod 2 (- 4)(- 6) 12(- 3) 1 2 7) (define (prod-inv a1 b1 c1 d1 a2 b2 c2 d2) not (= 0 (det2x2prod (a1 b1 c1 d1 a2 b2 c2

How to implement let as a lambda function in Scheme

半世苍凉 提交于 2019-12-25 14:46:33
问题 As an exercise I am trying to define let as a lambda function something like this: (define let_as_lambda (lambda (var) (lambda (value body) (var body) val))) And I am hoping to call it like this: ((let_as_lambda a) (3 (+ a 2))) However there is no way to pass an unbound variable (in this case "a") as an argument to a function. (I know it looks a little strange but I need let_as_lambda(var) to return a function.) Can anyone show me how to do this? Any advice is appreciated. In fact, just using

Return the second element for every element in a list

佐手、 提交于 2019-12-25 11:57:31
问题 Let's say we have this list '( (4 (1 2)) (5 (5 5)) (7 (3 1)) (1 (2 3))) I am trying to write smth in Scheme in order to get the second element for every element in the list.. So the result will look like '( (1 2) (5 5) (3 1) (2 3)) I have this code so far.. (define (second list1) (if (null? (cdr list1)) (cdr (car list1)) ((cdr (car list1))(second (cdr list1))))) 回答1: Here's a straightforward solution: (define (seconds lst) (map cadr lst)) In general, when you want to transform every element

How to use 'cons' without generating nested lists in Scheme?

痴心易碎 提交于 2019-12-25 09:44:12
问题 While attempting to generate a list of subsets that exist within two sets, I am running into an issue with cons . A procedure takes in a list named result and attempts to construct a new list out of result and the car of another set. So far, the set is generated with the correct elements, but they are contained within a size N nested list, where N is the number of nestings and the number of elements within the subset for which I am searching. How can I apply cons with result without creating

Trying to make a procedure called map-odd-mapper in scheme

坚强是说给别人听的谎言 提交于 2019-12-25 09:08:26
问题 I'm trying to make a procedure called map-odd-mapper where I take a proc that can then be applied to a list ex: ((make-odd-mapper add-one) (list 14 38 29 10 57)) (15 30 58) I was thinking of putting it as a let function as in (define (make-odd-mapper f) (let (..........something using ret-odds to allow for the indices so that you can get the odd numbers.... ret-odds is defined as (define (ret-odds lst) (if (null? lst) null (cons (car lst) (if (null? (cdr lst)) null (ret-odds (cdr (cdr lst))))

search through nested list in scheme to find a number

纵然是瞬间 提交于 2019-12-25 08:19:39
问题 How would I search through the nested list to find a certain number? For example, the list is: ((1 2) (2 3) (3 4) (3 5) (4 5)) and I'm looking for 1. Expected output: (1 2) since 1 is in the sub list (1 2). 回答1: First of all create function to flatten list. Something like this: > (flatten '((8) 4 ((7 4) 5) ((())) (((6))) 7 2 ())) (8 4 7 4 5 6 7 2) And then search your number in the ordinary list. This quesion looks like the homework so try to develop this function on your own and if you can