user2328195
user2328195

Reputation: 11

How do I create a list from a series of computed values (scheme)

I've created a function that gives a items in a list a certain score

(define newlist '((score 'A ) (score 'A1 ) (score 'A2 )))

but cannot get it to return a ( X Y Z) list . Only

'((score 23 ) (score 12 ) (score 7 )) which is only the substituted values for my variables.

Upvotes: 0

Views: 108

Answers (3)

GoZoner
GoZoner

Reputation: 70155

When you use quote (the character ') what follows in not evaluated. So in what you provided, the entire list of three items is not evaluated. To evaluate use list as such:

  (define newlist (list (score 'A) (score 'A1) (score 'A2)))

As @kmoerman points out, there are other ways, using map, to get a valid result; however, your original problem was using quote instead of list.

Upvotes: 0

amakarov
amakarov

Reputation: 524

(define newlist `(,(score 'A) ,(score 'A1) ,(score 'A2)))

Upvotes: 0

kmoerman
kmoerman

Reputation: 430

You could use the map function:

;if A, A1 and A2 are to be used as symbols:
(define newlist (map score '(A A1 A2)))
;which is equivalent to:
(define newlist (map score (list 'A 'A1 'A2)))

;however, if A, A1 and A2 are variables whose values you wish to use:
(define newlist (map score (list A A1 A2)))

Upvotes: 1

Related Questions