sicp-lang/sicp

segments->painter fails with list from #lang sicp

vdloo opened this issue · 10 comments

Hi,

I ran into a problem while doing exercise 2.49 from SICP. The segments->painter procedure from prmpnt.scm gives a contract violation if it's given a list defined in the sicp lang.

#lang sicp
(#%require sicp-pict)

(define (make-vect x y)
  (cons x y))

(define (xcor-vect vect)
  (car vect))

(define (ycor-vect vect)
  (cdr vect))

(define (make-segment vect-origin-to-start vect-origin-to-end)
  (cons vect-origin-to-start vect-origin-to-end))

(define outline-segments
  (segments->painter
   (list ; <- culprit
    (make-segment (make-vect 0.0 0.0) (make-vect 0.0 0.99))
    (make-segment (make-vect 0.0 0.0) (make-vect 0.99 0.0))
    (make-segment (make-vect 0.99 0.0) (make-vect 0.99 0.99))
    (make-segment (make-vect 0.0 0.99) (make-vect 0.99 0.99)))))

(paint outline-segments)


for-each: contract violation
  expected: list?
  given: (mcons (mcons '(0.0 . 0.0) '(0.0 . 0.99)) (mcons (mcons '(0.0 . 0.0) '(0.99 . 0.0)) (mcons (mcons '(0.99 . 0.0) '(0.99 . 0.99)) (mcons (mcons '(0.0 . 0.99) '(0.99 . 0.99)) '()))))
  argument position: 2nd

The violation occurs here. The snippet above works if instead of using #lang sicp, #lang racket/base is used.

Hi Rick,

This is a bit subtle. The language #lang sicp uses mutable pairs while
#lang racket uses immutable pairs.
If you create a list in #lang sicp you will get a list consisting of
mutable pairs (printed as mcons) and
since the segments->painter is expecting a Racket list consisting of
immutable pairs (printed as cons) you get
the error.

Best advice: Use #lang racket to work with the painter language.

/Jens Axel

2016-03-26 12:55 GMT+01:00 Rick van de Loo notifications@github.com:

Hi,

I ran into a problem while doing exercise 2.49 from SICP. The
segments->painter procedure from prmpnt.scm gives a contract violation if
it's given a list defined in the sicp lang.

#lang sicp
(#%require sicp-pict)

(define (make-vect x y)
(cons x y))

(define (xcor-vect vect)
(car vect))

(define (ycor-vect vect)
(cdr vect))

(define (make-segment vect-origin-to-start vect-origin-to-end)
(cons vect-origin-to-start vect-origin-to-end))

(define outline-segments
(segments->painter
(list ; <- culprit
(make-segment (make-vect 0.0 0.0) (make-vect 0.0 0.99))
(make-segment (make-vect 0.0 0.0) (make-vect 0.99 0.0))
(make-segment (make-vect 0.99 0.0) (make-vect 0.99 0.99))
(make-segment (make-vect 0.0 0.99) (make-vect 0.99 0.99)))))

(paint outline-segments)

for-each: contract violation
expected: list?
given: (mcons (mcons '(0.0 . 0.0) '(0.0 . 0.99)) (mcons (mcons '(0.0 . 0.0) '(0.99 . 0.0)) (mcons (mcons '(0.99 . 0.0) '(0.99 . 0.99)) (mcons (mcons '(0.0 . 0.99) '(0.99 . 0.99)) '()))))
argument position: 2nd

The violation occurs here
https://github.com/sicp-lang/sicp/blob/master/sicp-pict/prmpnt.scm#L114.
The snippet above works if instead of using #lang sicp, #lang racket/base
is used.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#4

Jens Axel Søgaard

I'll see if I can get segments->painter to accept both kind of lists.

/Jens Axel

2016-03-26 14:29 GMT+01:00 Jens Axel Søgaard jensaxel@soegaard.net:

Hi Rick,

This is a bit subtle. The language #lang sicp uses mutable pairs while
#lang racket uses immutable pairs.
If you create a list in #lang sicp you will get a list consisting of
mutable pairs (printed as mcons) and
since the segments->painter is expecting a Racket list consisting of
immutable pairs (printed as cons) you get
the error.

Best advice: Use #lang racket to work with the painter language.

/Jens Axel

2016-03-26 12:55 GMT+01:00 Rick van de Loo notifications@github.com:

Hi,

I ran into a problem while doing exercise 2.49 from SICP. The
segments->painter procedure from prmpnt.scm gives a contract violation if
it's given a list defined in the sicp lang.

#lang sicp
(#%require sicp-pict)

(define (make-vect x y)
(cons x y))

(define (xcor-vect vect)
(car vect))

(define (ycor-vect vect)
(cdr vect))

(define (make-segment vect-origin-to-start vect-origin-to-end)
(cons vect-origin-to-start vect-origin-to-end))

(define outline-segments
(segments->painter
(list ; <- culprit
(make-segment (make-vect 0.0 0.0) (make-vect 0.0 0.99))
(make-segment (make-vect 0.0 0.0) (make-vect 0.99 0.0))
(make-segment (make-vect 0.99 0.0) (make-vect 0.99 0.99))
(make-segment (make-vect 0.0 0.99) (make-vect 0.99 0.99)))))

(paint outline-segments)

for-each: contract violation
expected: list?
given: (mcons (mcons '(0.0 . 0.0) '(0.0 . 0.99)) (mcons (mcons '(0.0 . 0.0) '(0.99 . 0.0)) (mcons (mcons '(0.99 . 0.0) '(0.99 . 0.99)) (mcons (mcons '(0.0 . 0.99) '(0.99 . 0.99)) '()))))
argument position: 2nd

The violation occurs here
https://github.com/sicp-lang/sicp/blob/master/sicp-pict/prmpnt.scm#L114.
The snippet above works if instead of using #lang sicp, #lang racket/base
is used.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#4

Jens Axel Søgaard

Jens Axel Søgaard

This commit
0da6a81

fixes the problem.

Update the sicp package either using raco or the DrRacket package manager
to get the changes.

Thanks for the bug report.

/Jens Axel

2016-03-26 14:36 GMT+01:00 Jens Axel Søgaard jensaxel@soegaard.net:

I'll see if I can get segments->painter to accept both kind of lists.

/Jens Axel

2016-03-26 14:29 GMT+01:00 Jens Axel Søgaard jensaxel@soegaard.net:

Hi Rick,

This is a bit subtle. The language #lang sicp uses mutable pairs while
#lang racket uses immutable pairs.
If you create a list in #lang sicp you will get a list consisting of
mutable pairs (printed as mcons) and
since the segments->painter is expecting a Racket list consisting of
immutable pairs (printed as cons) you get
the error.

Best advice: Use #lang racket to work with the painter language.

/Jens Axel

2016-03-26 12:55 GMT+01:00 Rick van de Loo notifications@github.com:

Hi,

I ran into a problem while doing exercise 2.49 from SICP. The
segments->painter procedure from prmpnt.scm gives a contract violation if
it's given a list defined in the sicp lang.

#lang sicp
(#%require sicp-pict)

(define (make-vect x y)
(cons x y))

(define (xcor-vect vect)
(car vect))

(define (ycor-vect vect)
(cdr vect))

(define (make-segment vect-origin-to-start vect-origin-to-end)
(cons vect-origin-to-start vect-origin-to-end))

(define outline-segments
(segments->painter
(list ; <- culprit
(make-segment (make-vect 0.0 0.0) (make-vect 0.0 0.99))
(make-segment (make-vect 0.0 0.0) (make-vect 0.99 0.0))
(make-segment (make-vect 0.99 0.0) (make-vect 0.99 0.99))
(make-segment (make-vect 0.0 0.99) (make-vect 0.99 0.99)))))

(paint outline-segments)

for-each: contract violation
expected: list?
given: (mcons (mcons '(0.0 . 0.0) '(0.0 . 0.99)) (mcons (mcons '(0.0 . 0.0) '(0.99 . 0.0)) (mcons (mcons '(0.99 . 0.0) '(0.99 . 0.99)) (mcons (mcons '(0.0 . 0.99) '(0.99 . 0.99)) '()))))
argument position: 2nd

The violation occurs here
https://github.com/sicp-lang/sicp/blob/master/sicp-pict/prmpnt.scm#L114.
The snippet above works if instead of using #lang sicp, #lang
racket/base is used.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#4

Jens Axel Søgaard

Jens Axel Søgaard

Jens Axel Søgaard

Great! Thanks for the quick reply :)

GOOD GRIEF! I spent a whole day wondering what in the world was wrong with my code.

Thank you for the question and the answer. I wish I had checked sooner.

I can concur that declaring #lang racket instead of #lang sicp is the fix.

I pushed a commit to my repo, fixing the language declaration, and with details in the commit message.

Use 'racket', not 'sicp' in ex2-44 picture lang file adityaathalye/sicp@c8b62c3

Hmm. Provided that @soegaard made segments->painter to accept both mutable and immutable lists, there should not be any problem, right? Can you provide us a code example that leads to a problem?

Hmm. Provided that @soegaard made segments->painter to accept both mutable and immutable lists, there should not be any problem, right? Can you provide us a code example that leads to a problem?

Updated my earlier comment to keep things in context: #4 (comment)

That's a regression caused by me (see ce24736). I will push a fix soon, and you should be able to switch back to #lang sicp after that (feel free to keep using #lang racket, too!)

I believe this is fixed by 4152068. Closing. Feel free to reopen it if you find any other issues.

I just updated lang-sicp locally and can confirm SHA 4152068 fixes the bug. Thank you!