I've read several places that say it's really bad to use append, and I understand why. I read somewhere that it was never even correct to use append. However, I was working on a textbook exercise in Essentials of Programming Languages (Ex 1.21 pg27) that asked me to write a Cartesian product function. I couldn't think of any other easy way than using append-map. Below is my working solution to the exercise. Is there a better more idiomatic way of writing this? Thanks!

; product : Listof(SchemeVal) -> Listof(SchemeVal) -> Listof(List(SchemeVal))
; usage: (product '(a b c) '(x y)) -> '((a x) (a y) (b x) (b y) (c x) (c y))
(define (product lox loy)
  (append-map (lambda (x) (scalar x loy)) lox))
(define (scalar x loy)
  (map (lambda (y) (list x y)) loy))
  • \$\begingroup\$ Is this code copied verbatim from the text-book? How would it be used? \$\endgroup\$ – Mast Nov 15 '19 at 8:25
  • \$\begingroup\$ No this is my original code... Isn't copying other code against the terms of CR? And the usage is in the comment. \$\endgroup\$ – BrainFRZ Nov 15 '19 at 13:02
  • \$\begingroup\$ Yes it is, hence the question. Thank you for clarifying this. \$\endgroup\$ – Mast Nov 15 '19 at 13:04
  • 1
    \$\begingroup\$ No problem, sorry for the confusion. I've edited my question to make it more clear the code is mine, and where the question came from. \$\endgroup\$ – BrainFRZ Nov 15 '19 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.