I am trying to plot a set of values that includes both positive and negative numbers in Racket. I have tried using the discrete-histogram function from plot:
(plot
(discrete-histogram '(A B C) '(1.2 0.2 -0.4)
#:y-min -0.5))
The corresponding output doesn't plot anything for the negative value:
discrete-histogram has one required argument cat-vals, so you should use '((A 1.2) (B 0.2) (C -0.4)) or (if you already have list of X values and list of Y values) (map list '(A B C) '(1.2 0.2 -0.4)):
#lang racket
(require plot)
(plot (discrete-histogram
(map list '(A B C) '(1.2 0.2 -0.4))
#:y-min -0.5))
In both cases, I see this result:
Related
"A stem plot plots vertical lines at each x location from the baseline to y, and places a marker there."
Like this:
The graph you use is generated by the following code in Python:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 2 * np.pi, 41)
y = np.exp(np.sin(x))
plt.stem(x, y, use_line_collection=True)
plt.show()
Trying to build a similar plot using Racket:
#lang racket
(require plot)
(define my-linspace
(λ (start stop [num 50])
(range start stop (/ stop num))))
(define x (my-linspace 0 (* 2 pi) 41))
(define y (map exp (map sin x)))
(define my-blue '(32 119 180))
(plot
#:x-min -0.3
#:x-max 6.5
#:y-min -0.2
#:y-max 2.9
#:x-label #f
#:y-label #f
(let ([data (map list x y)])
(list
(points data #:color my-blue #:sym 'fullcircle5)
; for each data point, draw a vertical line
; at its "x" ranging from height 0 to its "y"
(list (map (λ (d) (vrule (first d) 0 (second d) #:color my-blue #:width 2)) data))
(list (hrule 0 (first (first data)) (first (last data)) #:color "red" #:width 2)))))
I have a question about a recursive function to draw an image
Yes, it's a homework, but I don't know how to draw that image. What I have so far is:
A function that gives me a list of points(x/y) from a curve((list (x1/y2) (x2/y2)...(xn/yn))
To get a x-coordinate for example I have to write point-x (first lis)
A function I called (zipWith f xs ys)that takes two lists(xs ys) apply the function f and makes a list (for example (zipWith + (list 1 2 3) (list 10 20 30)) -> (list 11 22 33))
A function make-tuple that takes two lists and makes them a tuple (for example:
(make-tuple (list 1 2 3) (list 4 5 6)) -> (tuple (list 1 2 3) (list 4 5 6))
To get the first tuple I have to write (xs1 (tuple (list 1 2 3) (list 4 5 6)) -> (list 1 2 3)
Now to the actual function:
We have to use the function add-line which draws a line from one point to another.
The parameters are image number number number number string
In other words: empty-image x1 y1 x2 y2 "black"
So it starts with an empty-image and draws a line from (x1/y1) to (x2/y2) with the color "black"
And we have to use fold to write the function. So my attempt on this:
;Signature
(: render ((list-of point) -> image))
(define render
(lambda (xs)
(let((tuple-list (zipWith make-tuple xs (rest xs))))
(fold empty-image
add-line
tuple-list))))
What I tried to do was take the list of points and make a list with tuple of points
(list (tuple (x1/y1) (x2/y2)) (tuple (x2/y2) (x3/y3))...(tuple (xn/yn) (xn/yn)))
Now I want to apply add-line with fold to the list of tuples. So that I say, lets take the first tuple (my two points) and apply them to the function add-line. Then take the next tuple and also apply these points to the add-line function (and so on) until my list is empty.
The problem now is, Scheme says add-line expects 6 parameters but found only 2. I know my problem, because add-line don't know where to get the other parameters. So I tried:
(define render
(lambda (xs)
(let((tuple-list (zipWith make-tuple xs (rest xs))))
(fold empty-image (add-line
empty-image
(point-x (xs1 (first tuple-list)))
(point-y (xs1 (first tuple-list)))
(point-x (xs2 (first tuple-list)))
(point-y (xs2 (first tuple-list)))
"black")
tuple-list))))
and know it says "function call: expected a function after the open parenthesis, but received an image"
And again I know my mistake, add-line draws one line, and fold tries to apply that image to the list, but need a function not an image. So my question:
How can I write a recursive function render with add-line and fold, that draws the first line, then get the next points and draws the next line combined with the first one?
(like a recursive add-line function, that calls itself again after drawing a line)
You don't say specifically which language you are using,
but this looks suspicious:
(fold empty-image (add-line ...))
In Racket the way to use fold is:
(foldl f base l)
Here f is a function. In your code you have empty-image.
You need to define define a function say f, that use a tuple to add a line onto the "current" image:
; f : tuple image -> image
(define (f t img)
(add-line img
(point-x (xs1 t))
(point-y (xs1 t))
(point-x (xs2 t))
(point-y (xs2 t))
"black"))
and then
(foldl f empty-image tuple-list)
You might need to tweak a thing or two to get it to work.
After spending some time looking at the documentation, searching the web, and experimenting at the prompt, I haven't succeeded at plotting points using Racket. Could someone post an example of how I'd go about plotting (0 1 2 3 4 5) for my x coordinates and (0 1 4 9 16 25) for the y coordinates. I think 1 good example will clear the problem up.
Based on the first example of the doc, and given that the function you want to plot already exists in Racket, it's as simple as:
(require plot)
(plot (function sqr 0 5 #:label "y = x ^ 2"))
If you just want to see the individual points, this is also taken from the docs:
(require plot)
(define xs '(0 1 2 3 4 5))
(define ys '(0 1 4 9 16 25))
(plot (points (map vector xs ys) #:color 'red))
which is equivalent to
(require plot)
(plot (points '(#(0 0) #(1 1) #(2 4) #(3 9) #(4 16) #(5 25)) #:color 'red))
I am practicing for my programming paradigms exam and working through problem sets I come to this problem. This is the first problem after reversing and joining lists recursively, so I suppose there is an elegant recursive solution.
I am given a list of lists and a permutation. I should permute every list including a list of lists with that specified permutation.
I am given an example:
->(permute '((1 2 3) (a b c) (5 6 7)) '(1 3 2))
->((1 3 2) (5 7 6) (a c b))
I have no idea even how to start. I need to formulate the problem in recursive interpretation to be able to solve it, but I can not figure out how.
Well, let's see how we can break this problem down. We are given a list of lists, and a list of numbers, and we want to order each list according to the order specified by the list of numbers:
=>(permute '((1 2 3) (4 5 6)) '(3 2 1))
'((3 2 1) (6 5 4))
We can see that each list in the list of lists can be handled separately, their solutions are unrelated to each other. So we can have a helper permute1 that handles the case of one list, then use map to apply this function to each of the lists (with the same ordering each time):
(define (permute lists ordering)
(map (lambda (xs) (permute1 xs ordering))
lists))
(define (permute1 items ordering)
...)
Now, to calculate (permute1 '(4 5 6) '(3 2 1)), what we mean is:
The first item of the new list will be the 3rd item of items, because the first number in ordering is 3.
The rest of the items of the new list will be determined by using the rest of the numbers in the ordering.
If the ordering is the empty list, return the empty list.
This forms the base case (3), the recursive case (1), and the steps to recur deeper (2). So a sketch of our solution would look like:
(define (permute1 items ordering)
(if (empty? ordering)
'()
(let ([next-item ???])
(??? next-item
(permute1 items (rest ordering))))))
Where the ???s represent getting the item based on the first number in ordering and combining this item with the remainder of the calculation, respectively.
Here's another option, using higher-order functions. This is the idiomatic way to think about a solution in a functional language - we split the problem in sub-problems, solve each one using existing procedures and finally we compose the answer:
(define (atom? x)
(and (not (null? x))
(not (pair? x))))
(define (perm lst order)
(foldr (lambda (idx acc)
(cons (list-ref lst (sub1 idx)) acc))
'()
order))
(define (permute lst order)
(if (atom? lst)
lst
(perm (map (lambda (x) (permute x order)) lst)
order)))
We start by defining atom?, a generic predicate and perm, a helper that will reorder any given list according to the ordering specified in one of its parameters. It uses foldr to build the output list and list-ref to access elements in a list, given its 0-based indexes (that's why we subtract one from each index).
The main permute function takes care of (recursively) mapping perm on each element of an arbitrarily nested input list, so we can obtain the desired result:
(permute '((1 2 3) (a b c) (5 6 7)) '(1 3 2))
=> '((1 3 2) (5 7 6) (a c b))
I am given an example:
(permute ('(1 2 3) '(a b c) '(5 6 7)) '(1 3 2))
((1 3 2) (5 7 6) (a c b))
The syntax you've given isn't correct, and will cause an error, but it's fairly clear what you mean. You want that
(permute '((1 2 3) (a b c) (5 6 7)) '(1 3 2))
;=> ((1 3 2) (5 7 6) (a c b))
Now, it's not clear how you're indicating the permutation. Is '(1 3 2) a permutation because it has some (1-based) indices, and indicates the way to rearrange elements, or is it because it is actually a permutation of the elements of the first list of the first list? E.g., would
(permute '((x y z) (a b c) (5 6 7)) '(1 3 2))
;=> ((x z y) (5 7 6) (a c b))
work too? I'm going to assume that it would, because it will make the problem much easier.
I have no idea even how to start. I need to formulate the problem in
recursive interpretation to be able to solve it, but I can not figure
out how.
You need to write a function that can take a list of indices, and that returns a function that will perform the permutation. E.g,.
(define (make-permutation indices)
…)
such that
((make-permutation '(3 1 2)) '(a b c))
;=> (c a b)
One you have that, it sounds like your permute function is pretty simple:
(define (permute lists indices)
(let ((p (make-permutation indices)))
(p (map p lists))))
That would handle the case you've given in your example, since (map p lists) will return ((1 3 2) (a b c) (5 7 6)), and then calling p with that will return ((1 3 2) (5 7 6) (a c b)). If you need to be able to handle more deeply nested lists, you'll need to implement a recursive mapping function.
Here's my take, which seems to be shorter than the previous examples:
(define (permute lst ord)
(define ord-1 (map sub1 ord)) ; change from 1-based to 0-based indexes
(define (perm elts) ; recursive sub-procedure
(if (list? elts)
(map perm (map (curry list-ref elts) ord-1)) ; list -> recurse
elts)) ; else return unchanged
(perm lst)) ; initial call
testing
> (permute '((1 2 3) (a b c) (5 6 7)) '(1 3 2))
'((1 3 2) (5 7 6) (a c b))
> (permute '((1 (i permute did) 3) (a b (scheme cool is)) (5 6 7)) '(1 3 2))
'((1 3 (i did permute)) (5 7 6) (a (scheme is cool) b))
I'm doing a tut on lisp http://common-lisp.net/language.html#sec-1
and am wondering how would this function be written:
(my-floor 1.3)
=> 1 0.3
Use values:
(defun foo (x y)
(values x y (+ x y) (cons x y)))
Try the function:
> (foo 2 pi)
2 ;
3.1415926535897932385L0 ;
5.1415926535897932383L0 ;
(2 . 3.1415926535897932385L0)
Use the returned values with multiple-value-bind:
(multiple-value-bind (a b sum pair) (foo 1 2)
(list a b sum pair))
==> (1 2 3 (1 . 2))
or (setf values).
See also values function in Common Lisp.