Translating Python into Scheme/Racket - python-2.7

I'm currently trying to translate this python 2 code:
import math
def worstCaseArrayOfSize(n):
if n == 1:
return [1]
else:
top = worstCaseArrayOfSize(int(math.floor(float(n) / 2)))
bottom = worstCaseArrayOfSize(int(math.ceil(float(n) / 2)))
return map(lambda x: x * 2, top) + map(lambda x: x * 2 - 1, bottom)
into racket/scheme code, and having a difficult time.
This is what I have so far:
(define (msortWorstCase n)
(cond
[(equal? 1 n) 1]
[else (let* ([top (msortWorstCase(floor (/ n 2)))] [bottom (msortWorstCase (ceiling (/ n 2)))])
(append (map (lambda (x) (* x 2)) (list top)) (map (lambda (x) (- (* x 2) 1)) (list bottom))))]
)
)
Could anyone tell me where I am going wrong with this?
I am getting the following error:
*: contract violation
expected: number?
given: '(2 1)
argument position: 1st
other arguments...:

Your recursion is making lists of lists of lists of lists... with (list top) and (list bottom).
You should do the same thing in Racket as you did in Python; the base case should be a one-element list, and you should not wrap the results in lists in the recursive case.
(define (msortWorstCase n)
(cond
[(equal? 1 n) '(1)]
[else (let* ([top (msortWorstCase(floor (/ n 2)))]
[bottom (msortWorstCase (ceiling (/ n 2)))])
(append (map (lambda (x) (* x 2)) top)
(map (lambda (x) (- (* x 2) 1)) bottom)))]))

Related

Concatenating list elements - Scheme

If i have a scheme code that generates the following result: (i'm using cons)
'((1 . 0) . 0)
How can i take this, and just simply display 100 as if it were just one integer number and not a list presented with those dots and parenthesis?
Thanks!
EDIT:
my full code:
(define (func X)
(if ( <= X 3 )
X
(cons (modulo X 4) (func(floor(/ X 4)) ))
))
If I understand correctly, you're trying to convert a number from base 10 to base 4, and then display it as a number, but there are several problems with your implementation.
You're building a list as output - but that's not what you want, you want a number. Also, you're traversing the input in the wrong order, and that's not the correct way to find the quotient between two numbers. Perhaps this will help:
(define (func X)
(let loop ((n X) (acc 0) (mult 1))
(if (< n 4)
(+ (* mult n) acc)
(loop (quotient n 4)
(+ (* mult (modulo n 4)) acc)
(* mult 10)))))
Alternatively, you could output a string to stress the fact that the output is not in base 10:
(define (func X)
(let loop ((n X) (acc ""))
(if (< n 4)
(string-append (number->string n) acc)
(loop (quotient n 4)
(string-append (number->string (modulo n 4)) acc)))))
It'll work as expected:
(func 16)
=> 100
Oscar Lopez's answer is excellent. I can't help adding that this problem doesn't need the "loop" construct:
;; translate a string to a base-4 string.
(define (func n)
(cond [(< n 4) (number->string n)]
[else (string-append (func (quotient n 4))
(number->string (modulo n 4)))]))

Multiply 1st element of every sublist by -1?? Common Lisp

As part of a larger project I want to be able to multiple every first element of every sublistby -1. I am trying to do it using recursion at the moment like this:
(defun negative (secondpoly)
(let ((t1(car secondpoly))
(rem(cdr secondpoly)))
(let ((c1(car t1)))
(if (not (null (cdr secondpoly)))
(negative (append (* c1 -1) rem))
)
)
)
)
With this input:
(pminus '((5 x 2)(100 x 2)))
I want to receive this output:
(pminus '((-5 x 2)(-100 x 2)))
I was wondering if someone could show me a way to do this?
If your input really is something like: '(pminus ((5 x 2)(100 x 2)))
(defun negative-first (poly)
(cons (car poly)
(mapcar (lambda (el)
(cons (- (car el)) (cdr el)))
(cadr poly))))
CL-USER> (negative-first '(pminus ((5 x 2)(100 x 2))))
(PMINUS (-5 X 2) (-100 X 2))
But for me it looks like there is a mistake in explanation, and your input is '((5 x 2)(100 x 2)) and if so, your function is:
(defun negative-first (poly)
(mapcar (lambda (el)
(cons (- (car el)) (cdr el)))
poly))
CL-USER> (negative-first '((5 x 2)(100 x 2)))
((-5 X 2) (-100 X 2))
If you need to work with more nested and complex data, you probably should who some examples, this function will work only with one level ((5 x 2) (100 x 2) (40 x 3) .... ).

Create list from 2 numbers and 2 for loops

I have 2 numbers let's say number1=5 and number2=3 and I want to create a list in this form
((1(1 2 3)) (2(1 2 3)) (3(1 2 3)) (4(1 2 3)) (5(1 2 3)))
So the number1 indicates the number of the elements in the list and number2 indicates the total elements that will be as the second part of every element..
I have smth like this untill now
(define mylist '())
(define (pushlist item item2)
(do ((j 1 (+ j 1))) ((> j item2))
(set! mylist(list mylist (list item j)))))
(define (createlist number number2)
(do ((j 1 (+ j 1))) ((> j number))
(pushlist j number2)
))
(createlist 5 3)
Unfortunately it doesn't work.. It doesn't give the result I want.. It gives me this (((((((((((((((() (1 1)) (1 2)) (1 3)) (2 1)) (2 2)) (2 3)) (3 1)) (3 2)) (3 3)) (4 1)) (4 2)) (4 3)) (5 1)) (5 2)) (5 3))
There are many ways to solve this problem - for example, using explicit recursion, or using higher-order procedures. Your approach is not recommended, in Scheme you should try to avoid thinking about loops and mutation operations. Although it is possible to write such a solution, it won't be idiomatic. I'll try to explain how to write a more idiomatic solution, using explicit recursion first:
; create a list from i to n
(define (makelist i n)
(if (> i n)
'()
(cons i (makelist (add1 i) n))))
; create a list from i to m, together with
; a list returned by makelist from 1 to n
(define (makenumlist i m n)
(if (> i m)
'()
(cons (list i (makelist 1 n))
(makenumlist (add1 i) m n))))
; call previous functions
(define (createlist number1 number2)
(makenumlist 1 number1 number2))
Now, an even more idiomatic solution would be to use higher-order procedures. This will work in Racket:
; create a list from i to n
(define (makelist n)
(build-list n add1))
; create a list from i to m, together with
; a list returned by makelist from 1 to n
(define (makenumlist m n)
(build-list m
(lambda (i)
(list (add1 i) (makelist n)))))
; call previous functions
(define (createlist number1 number2)
(makenumlist number1 number2))
See how we can avoid explicit looping? that's the Scheme way of thinking, the way you're expected to solve problems - embrace it!
I don't think that your pushlist procedure is doing what you you expect it to.
(define (pushlist item item2)
(do ((j 1 (+ j 1)))
((> j item2))
(set! mylist (list mylist (list item j)))))
If you have a list (x y z) and you want to push a new value v into it, you'd do
(set! lst (cons v lst))
because (cons v (x y z)) == (v x y z). By doing
(set! mylist (list mylist (list item j)))
you're making mylist always have exactly two elements, where the first is a deeper and deeper nested list. Óscar López's answer gives a more idiomatic approach to this problem. Here's a similar idiomatic approach:
(define (range n)
; returns a list (1 ... n)
(let rng ((n n) (l '()))
(if (zero? n)
l
(rng (- n 1) (cons n l)))))
If the sublists (1 ... n) can all be the same list (i.e., the actual list object is the same), then you can create it just once:
(define (createlist m n)
(let ((sublist (range n)))
(map (lambda (main)
(list main sublist))
(range m))))
Otherwise, if they need to be distinct, you can generate one for each of 1 ... m:
(define (createlist m n)
(map (lambda (main)
(list main (range n)))
(range m)))

Adding values to a list in a sort of "overlapped" way

I'll explain in math, here's the transformation I'm struggling to write Scheme code for:
(f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) cf)
Where two letters together like ad means (* a d).
I'm trying to write it in a purely functional manner, but I'm struggling to see how. Any suggestions would be greatly appreciated.
Here are some examples:
(1mul '(0 1) '(0 1)) = '(0 0 1)
(1mul '(1 2 3) '(1 1)) = '(1 3 5 3)
(1mul '(1 2 3) '(1 2)) = '(1 4 7 6)
(1mul '(1 2 3) '(2 1)) = '(2 5 8 3)
(1mul '(1 2 3) '(2 2)) = '(2 6 10 6)
(1mul '(5 5 5) '(1 1)) = '(5 10 10 5)
(1mul '(0 0 1) '(2 5)) = '(0 0 2 5)
(1mul '(1 1 2 3) '(2 5)) = '(2 7 9 16 15)
So, the pattern is like what I posted at the beginning:
Multiply the first number in the list by every number in the second list (ad, ae, af) and then continue along, (bd, be, bf, cd, ce, cf) and arrange the numbers "somehow" to add the corresponding values. The reason I call it overlapping is because you can sort of visualize it like this:
(list
aa'
(+ ba' ab')
(+ ca' bb' ac')
(+ cb' bc')
cc')
Again,
(f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) cf)
However, not just for 3x3 lists, for any sized lists.
Here's my code. It's in racket
#lang racket
(define (drop n xs)
(cond [(<= n 0) xs]
[(empty? xs) '()]
[else (drop (sub1 n) (rest xs))]))
(define (take n xs)
(cond [(<= n 0) '()]
[(empty? xs) '()]
[else (cons (first xs) (take (sub1 n) (rest xs)))]))
(define (mult as bs)
(define (*- a b)
(list '* a b))
(define degree (length as))
(append
(for/list ([i (in-range 1 (+ 1 degree))])
(cons '+ (map *- (take i as) (reverse (take i bs)))))
(for/list ([i (in-range 1 degree)])
(cons '+ (map *- (drop i as) (reverse (drop i bs)))))))
The for/lists are just ways of mapping over a list of numbers and collecting the result in a list. If you need, I can reformulate it just maps.
Is this a good candidate for recursion? Not sure, but here's a
a direct translation of what you asked for.
(define (f abc def)
(let ((a (car abc)) (b (cadr abc)) (c (caddr abc))
(d (car def)) (e (cadr def)) (f (caddr def)))
(list (* a d)
(+ (* b d) (* a e))
(+ (* c d) (* b e) (* a f))
(+ (* c e) (* b f))
(* c f))))
Is it correct to assume, that you want to do this computation?
(a+b+c)*(d+e+f) = a(d+e+f) + b(d+e+f) + c(d+e+f)
= ad+ae+af + bd+be+bf + cd+ce+cf
If so, this is simple:
(define (f xs ys)
(* (apply + xs) (apply + ys))
If you are interested in the symbolic version:
#lang racket
(define (f xs ys)
(define (fx x)
(define (fxy y)
(list '* x y))
(cons '+ (map fxy ys)))
(cons '+ (map fx xs)))
And here is a test:
> (f '(a b c) '(d e f))
'(+ (+ (* a d) (* a e) (* a f))
(+ (* b d) (* b e) (* b f))
(+ (* c d) (* c e) (* c f)))

Not a Procedure, Really?

I been trying to figure this out for like 2 hours now! please Consider the following code:
(define (PowListF list)
(PowListFHelp list (- (length list) 1)))
(define (FuncPow f n)
(if (= 0 n)
f
(lambda (x)
(FuncPow (f (- n 1)) x))))
(define (PowListFHelp list n)
(if (= n 0)
(list (list-ref list 0))
(cons (FuncPow (list-ref list n) n)
(PowListFHelp list (- n 1)))))
The rocket Scheme compilers give me error:
application: not a procedure;
expected a procedure that can be applied to arguments
given: (# #)
arguments...:
#
and it points to the (cons part...
and The following code, that uses the same if structure, does work:
(define (polyCheb n)
(reverse (polyChebHelp n)))
(define (polyChebHelp n)
(if (= n 0)
(list (polyChebFunc n))
(cons (polyChebFunc n)
(polyChebHelp (- n 1)))))
(define (polyChebFunc n)
(if (= n 0)
(lambda (x) 1)
(if (= n 1)
(lambda (x) x)
(lambda (x)
(- (* (* 2 x)
((polyChebFunc(- n 1)) x))
((polyChebFunc(- n 2)) x))))))
EDIT: PowListF is being given list of functions as parameter, returns the same list s.t every function is composed with itself (its index + 1) times...
usage:
((list-ref (PowListF (list (lambda (x) x) (lambda (x) (expt x 2)))) 1) 2)
value should be 2^2^2=2^4=16
EDIT 2:
This was the solution :
(define (FuncPow f n)
(if (= 0 n)
f
(lambda (x) (f ((FuncPow f (- n 1)) x)))))
The problem lies with: (FuncPow(f (- n 1)) x)
The result of calling f must be procedure (I assume this as you return a procedure).
You further then 'assign' f as: (list-ref list n).