For Programmers: Free Programming Magazines  


Home > Archive > Scheme > December 2006 > Desperately need advice about a foldr function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Desperately need advice about a foldr function
Odinn

2006-12-03, 7:08 pm

Greetings , I am trying to write a program that takes a list of posns
and by these posns(points) making a polygon by connecting them with
lines. Less or more I have written majority of the work however I am
stucked at a part that data sending for the recursion of the function
requires posn struct however the program is sending image thus giving
error. I am using a hybrid teachpack of world.ss and draw.ss from my
universty for using add-line and empty-scene function.
I will be greatful if someone correcting my program.
Thanks in advance.

PS: I have read the 1.2.3 part of the HTDP however still didnt help
much.

;;posn struct
;; x represents x coordinate of a point
;; y represents y coordinate of a point
(define-struct posn
(x y))
;;defining several posn structs
(define p1 (make-posn 250 250))
(define p2 (make-posn 100 80))
(define p3 (make-posn 90 160))
(define p4 (make-posn 350 350))
(define p5 (make-posn 320 110))
;;a list of posn structs
(define point-list (list p1 p2 p3 p4 p5))

;; posn struct , posn struct -> image
;;consumes 2 points and connects them by a line.
(define draw-line
(lambda (image point1 point2)
(add-line image (posn-x point1) (posn-y point1) (posn-x point2)
(posn-y point2) 'black))
)
;;ex: (draw-line (empty-scene 500 500) p1 p2)


(foldr (lambda (a b) (draw-line (draw-line (empty-scene 500 500) a b) a
b)) p1 (list p1 p2 p3 p4 p5))

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com