racket: pict to bytes (pixels) - racket

How can I turn a pict into a byte string of the pixels in it?
(I mean, a byte string of the pixels that would be in the bitmap you'd get from rendering the pict)
This function works:
#lang racket/base
(require pict racket/class)
(define (pict->pixels p)
(define b (pict->bitmap p))
(define w (send b get-width))
(define h (send b get-height))
(define pixbuf (make-bytes (* 4 (inexact->exact (ceiling (* w h))))))
(send b get-argb-pixels 0 0 w h pixbuf)
pixbuf)
But is there a simpler way?

Yes! There is a simpler way: pict->argb-pixels
Here's a test case:
#lang racket/base
(require pict racket/class rackunit)
(define (pict->pixels p)
(define b (pict->bitmap p))
(define w (send b get-width))
(define h (send b get-height))
(define pixbuf (make-bytes (* 4 (inexact->exact (ceiling (* w h))))))
(send b get-argb-pixels 0 0 w h pixbuf)
pixbuf)
(define my-pict (jack-o-lantern 100))
(check-equal? (pict->pixels my-pict) (pict->argb-pixels my-pict))

Related

Turning a vector into a list and then into an image in Racket

So, my brain is fried and in class we are working on Conway's Game of Life in DrRacket. This is an Intro to CS class so this is proving to be difficult for me, since coding is very new to me.
I have used lists in animation before but I am lost as to how to turn a vector into an image. Our prof gave us the hint of turning a vector into a list and then we should be able to create the image. I can turn the vector into a list but then I get lost. Any help, guidance or advice would be so greatly appreciated...greatly greatly appreciated.
This isn't all of the code, just a sample.
(define small-board (vector
(vector 1 0)
(vector 0 1))
)
(define live-square (square 10 "solid" "blue"))
(define dead-square (square 10 "solid" "red"))
;Purpose: Create a function that turns board into an image
;Signature: Vector of Vectors -> Image
;Example
(check-expect (board->image small-board)
(above (beside live-square dead-square)
(beside dead-square live-square))
)
;Code
(define (board->image brd)
...
Since you can turn the 2d vector into a 2d list, I can show you how to turn the 2d list into an image.
(require 2htdp/image)
(define small-board (vector (vector 1 0) (vector 0 1)))
(define small-board-as-list (list (list 1 0) (list 0 1)))
(define live-square (square 10 "solid" "blue"))
(define dead-square (square 10 "solid" "red"))
(define MT empty-image)
Recur over the board and put every rendered row above the rest of the rendered board. Within a helper, recur over the row and put each rendered cell beside the rendered "rest" of the row.
; [List-of [List-of (U 1 0)]] -> Image
(define (board->image b)
(cond [(empty? b) MT]
[else (above (row->image (first b))
(board->image (rest b)))]))
; [List-of (U 1 0)] -> Image
(define (row->image r)
(cond [(empty? r) MT]
[else (beside (cell->image (first r))
(row->image (rest r)))]))
; Cell -> Image
(define (cell->image c)
(if (= 1 c) live-square dead-square))
The recursive structure can be abstracted using foldr:
; [List-of [List-of (U 1 0)]] -> Image
(define (board->image-abs.v1 b)
(foldr (λ (r b) (above (foldr (λ (c r) (beside (cell->image c) r)) MT r) b)) MT b))
We can also use map and apply
; [List-of [List-of (U 1 0)]] -> Image
(define (board->image-abs.v2 b)
(apply above (map (λ (r) (apply beside (map (λ (c) (cell->image c)) r))) b)))
The result
(board->image small-board-as-list)
(board->image-abs.v1 small-board-as-list)
(board->image-abs.v2 small-board-as-list)

For loop which prints out every 3rd number

I'm trying to make a for loop that iterates over a list of numbers and prints out every 3rd number.
Edit: I've only figured out how to use the for loop but I'm not entirely sure if there's a specific function I can use to only show every 3rd number. I feel like I might be on the right path when using car/cdr function except I'm getting an error
rest: contract violation
expected: (and/c list? (not/c empty?))
given: 0
My code:
(for/list ([x (in-range 20)] #:when (car(cdr(cdr x)))) (displayln x))
I'm trying to make a for loop that iterates over a list of numbers and prints out every 3rd number.
Typically it is more useful to create a new list with the desired values, and then print those values, or pass them to a function, or do whatever else may be needed. for/list does indeed return a list, and this is one reason for problems encountered by OP example code. (Other problems in OP code include that x is a number with [x (in-range 20)], so (cdr x) is not defined).
A possible solution would be to recurse over the input list, using take to grab the next three values, keeping the third, and using drop to reduce the input list:
;; Recurse using `take` and `drop`:
(define (every-3rd-1 lst)
(if (< (length lst) 3)
'()
(cons (third (take lst 3))
(every-3rd-1 (drop lst 3)))))
Another option would be to recurse on the input list using an auxiliary counter; starting from 1, only keep the values from the input list when the counter is a multiple of 3:
;; Recurse using an auxilliary counter:
(define (every-3rd-2 lst)
(define (every-3rd-helper lst counter)
(cond [(null? lst)
'()]
[(zero? (remainder counter 3))
(cons (first lst) (every-3rd-helper (rest lst) (add1 counter)))]
[else (every-3rd-helper (rest lst) (add1 counter))]))
(every-3rd-helper lst 1))
Yet another possibility would be to use for/list to build a list; here i is bound to values from the input list, and counter is bound to values from a list of counting numbers:
;; Use `for/list` to build a list:
(define (every-3rd-3 lst)
(for/list ([i lst]
[counter (range 1 (add1 (length lst)))]
#:when (zero? (remainder counter 3)))
i))
This function (or any of them, for that matter) could be usefully generalized to keep every nth element:
;; Generalize to `every-nth`:
(define (every-nth n lst)
(for/list ([i lst]
[counter (range 1 (add1 (length lst)))]
#:when (zero? (remainder counter n)))
i))
Finally, map could be used to create a list containing every nth element by mapping over a range of every nth index into the list:
;; Use `map` and `range`:
(define (every-nth-map n lst)
(map (lambda (x) (list-ref lst x)) (range (sub1 n) (length lst) n)))
If what OP really requires is simply to print every third value, rather than to create a list of every third value, perhaps the code above can provide useful materials allowing OP to come to a satisfactory conclusion. But, each of these functions can be used to print results as OP desires, as well:
scratch.rkt> (for ([x (every-3rd-1 '(a b c d e f g h i j k l m n o p))])
(displayln x))
c
f
i
l
o
scratch.rkt> (for ([x (every-3rd-2 '(a b c d e f g h i j k l m n o p))])
(displayln x))
c
f
i
l
o
scratch.rkt> (for ([x (every-3rd-3 '(a b c d e f g h i j k l m n o p))])
(displayln x))
c
f
i
l
o
scratch.rkt> (for ([x (every-nth 3 '(a b c d e f g h i j k l m n o p))])
(displayln x))
c
f
i
l
o
scratch.rkt> (for ([x (every-nth-map 3 '(a b c d e f g h i j k l m n o p))])
(displayln x))
c
f
i
l
o
Here is a template:
(for ([x (in-list xs)]
[i (in-naturals]
#:when some-condition-involving-i)
(displayln x))

Racket, split a list in two different size lists

This code splits a list in two equal size lists:
(define (split ls)
(if (or (null? ls) (null? (cdr ls)))
(list ls '())
(let ((next (split (cddr ls))))
(list (cons (car ls) (car next))
(cons (cadr ls) (cadr next))))))
I would like to build a code (define (split size ls)) where size has a value, for example: 0.20, 0.50, 0.63 and it is the number (in %) that will go to the first list.
Here is one way to do it.
Instead of using the helper function split-at one could use your function split.
#lang racket
; clamp : number number -> number
; make sure x is in the interval [a;b],
; if not return a or b.
(define (clamp x a b)
(max (min x b) a))
; split-list : number list -> list list
; return two values:
; - appending the two lists will produce the a list equal to the input list xs
(define (split-list pct xs)
; find the position to split the list
(define pos (exact-round (* (clamp pct 0.0 1.0) (length xs))))
; split it
(split-at xs pos))
(split-list 0.00 '(a b c d))
(split-list 0.25 '(a b c d))
(split-list 0.50 '(a b c d))
(split-list 0.75 '(a b c d))
(split-list 1.00 '(a b c d))

Lisp recursive square use one variables

This is Trying code
(defun f (a n)
(if (zerop n)
1
(* a (f a (- n 1)))))
(f 3) should return 27, (f 4) should return 256
I tried using two variables, but it be against the rules.
Is it possible to use only one variable using recursive?
Thanks for any ideas
I don't know CL, but I do know Clojure and other languages that use recursion.
In cases where a recursive function has 1 parameter acting as an accumulator, but is only set on the first call, the typical way around this is to wrap f in another function. There are 2 (basically the same) ways of doing this:
(defun g (a n)
(if (zerop n)
1
(* a (g a (- n 1)))))
(defun f (n)
; I'm assuming you want the initial value of "a" to be 1
(g 1 n))
Or, more succinctly:
(defun f (n)
(let (g (fn (n)
(if (zerop n)
1
(* a (g a (- n 1))))))))
; Instead of f being recursive, f calls g, which is recursive
(g 1 n))
Excuse any syntax errors.
Using an additional variable to count down would be the sane choice, but you don't need to change the contract of just one numeric argument input just for this. You can make a helper to do that:
(defun exptnn (n)
"Get the (expt n n)"
(check-type n integer)
(labels ((helper (acc count)
(if (zerop count)
acc
(helper (* acc n) (1- count)))))
(if (< n 0)
(/ 1 (helper 1 (- n)))
(helper 1 n))))
Now to solve with without any helpers just with one argument is possible since there is a solution doing that already, but I must say that is like programming in Brainf*ck without the joy!
CL-USER 15 > (defun f (n)
(labels ((g (m)
(if (zerop m)
1
(* n (g (1- m))))))
(g n)))
F
CL-USER 16 > (f 0)
1
CL-USER 17 > (f 1)
1
CL-USER 18 > (f 2)
4
CL-USER 19 > (f 3)
27
CL-USER 20 > (f 4)
256
CL-USER 21 > (loop for i below 10 collect (f i))
(1 1 4 27 256 3125 46656 823543 16777216 387420489)
This is a solution where no functions with more than one parameter are used (except for =, +, *, logand, ash; note also that logand and ash always take a constant as second parameter so they can be implemented as unary functions too).
The idea is to "hide" the two parameters needed for the obvious recursive approach in a single integer using odd/even bits.
(defun pair (n)
(if (= n 0)
0
(+ (* 3 (logand n 1))
(ash (pair (ash n -1)) 2))))
(defun pair-first (p)
(if (= p 0)
0
(+ (logand p 1)
(ash (pair-first (ash p -2)) 1))))
(defun pair-second (p)
(pair-first (ash p -1)))
(defun subsec (p)
(if (= 2 (logand p 2))
(- p 2)
(+ (logand p 1) 2 (ash (subsec (ash p -2)) 2))))
(defun pairpow (p)
(if (= (pair-second p) 1)
(pair-first p)
(* (pair-first p)
(pairpow (subsec p)))))
(defun f (n)
(pairpow (pair n)))
No reasonable real use, of course; but a funny exercise indeed.
Yes, this is possible:
(defun f (n)
(cond
((numberp n)
(f (cons n n)))
((zerop (car n))
1)
(t
(* (cdr n)
(f (cons (1- (car n))
(cdr n)))))))
The trick is that you can store any data structure (including a pair of numbers) in a single variable.
Alternatively, you can use helpers from the standard library:
(defun f (n)
(apply #'*
(loop repeat n collect n)))
But that doesn't use recursion. Or simply:
(defun f (n)
(expt n n))

A elementary Lisp procedure error

(define (sum-two-sqrt a b c)
(cond ((and (<= c a) (<= c b)) sqrt-sum(a b))
((and (<= a b) (<= a c)) sqrt-sum(b c))
((and (<= b a) (<= b c)) sqrt-sum(a c))
)
)
(define (sqrt-sum x y)
(+ (* x x) (*y y))
)
(define (<= x y)
(not (> x y))
(sum-two-sqrt 3 4 5)
This is my code
Please help me to fix the problem. :)
I just start studing Lisp today.
learned some C before but the two language is QUITE DIFFERENT!
This is the question
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
If you have better algorithm
POST IT!
Thank you :)
There's no need to define <=, it's a primitive operation. After fixing a couple of typos:
sqrt-sum: you were incorrectly invoking the procedure; the opening parenthesis must be written before the procedure name, not after.
sqrt-sum: (*y y) is incorrect, you surely meant (* y y); the space(s) after an operator matter.
This should work:
(define (sqrt-sum x y)
(+ (* x x) (* y y)))
(define (sum-two-sqrt a b c)
(cond ((and (<= c a) (<= c b)) (sqrt-sum a b))
((and (<= a b) (<= a c)) (sqrt-sum b c))
((and (<= b a) (<= b c)) (sqrt-sum a c))))
Or another alternative:
(define (sum-two-sqrt a b c)
(let ((m (min a b c)))
(cond ((= a m) (sqrt-sum b c))
((= b m) (sqrt-sum a c))
(else (sqrt-sum a b)))))
Following up on a suggestion by #J.Spiral and seconded by #River, the following Racket code reads nicely to me:
#lang racket
(define (squares-of-larger l)
(define two-larger (remove (apply min l) l))
(for/sum ([i two-larger]) (* i i)))
(squares-of-larger '(3 1 4)) ;; should be 25
Please note that this solution is entirely functional, since "remove" just returns a new list.
Also note that this isn't even in the same neighborhood with HtDP; I just wanted to express this concisely, and show off for/sum.
I didn't have Scheme interpreter here, but below seems to be shorter then other suggestions :) So it's in CL, but should look very similar in Scheme.
(defun sum-two-sqrt (a b c)
(let ((a (max a b))
(b (max (min a b) c)))
(+ (* a a) (* b b))))
In Scheme this would translate to:
(define (sum-two-sqrt a b c)
(let ((a (max a b))
(b (max (min a b) c)))
(+ (* a a) (* b b))))
the algorithm seems to work, just turn
*y
to
* y
whitespace is important here, else you're telling the interpreter you want to usethe function *y
add a close paren after
(define (<= x y) (not (> x y))
sqrt-sum(a b)
turns to
(sqrt-sum a b)
and ditto for the other sqrt-sum calls
edit: also a possibility:
(define (square a) (* a a))
(define (square-sum a b c)
(- (+ (square a)
(square b)
(square c))
(square (min a b c))))