Not a Procedure, Really? - if-statement

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).

Related

How can I fix my Scheme error: Argument #1 '()' to 'car' has wrong type (empty-list)

Write a function that takes a list and a length as input and returns two lists: (1) The first length elements of the input list, and (2) the remainder of the input list. Hint: Use a helper method with an "accumulator" parameter. I'm stuck guys and could really use some help.
I keep getting error when I try to do (split-list '(a b c d e f g) 7) which is the number equal to length otherwise any number less than than that does what its supposed to do
:
Argument #1 '()' to 'car' has wrong type (empty-list)
(split-list '(a b c d e f g) 0) should return '(() (a b c d e f g))
(split-list '(a b c d e f g) 1) should return '((a) (b c d e f g))
(split-list '(a b c d e f g) 3) should return '((a b c) (d e f g))
(define (split-list lst length)
(define(split-list-head accum length)
(if (= length 0)
(cdr '(accum))
(cons (car accum) (split-list-head (cdr accum)(- length 1)))
)
)
(define(split-list-tail accum length)
(if (= length 0)
(cons (car accum)(cdr accum))
(split-list-tail (cdr accum)(- length 1))
)
)
(if (eq? length 0)
(append(list (list))(list lst))
(append(list(split-list-head lst length)) (list(split-list-tail lst length)))
)
)
Unless you are particularly attached to GNU Scheme, I would consider moving to Dr Racket.
Although it was written for a similar language called Racket, it can be set up to run vanilla Scheme, by putting in a first line #lang scheme
The nice about Dr Racket is that it has a very nice debugger.
My code, with the #lang line at the start, and the erroring line at the bottom:
#lang scheme
(define (split-list lst length)
(define(split-list-head accum length)
(if (= length 0)
(cdr '(accum))
(cons (car accum) (split-list-head (cdr accum)(- length 1)))
)
)
(define(split-list-tail accum length)
(if (= length 0)
(cons (car accum)(cdr accum))
(split-list-tail (cdr accum)(- length 1))
)
)
(if (eq? length 0)
(append(list (list))(list lst))
(append(list(split-list-head lst length)) (list(split-list-tail lst length)))
)
)
(split-list '(a b c d e f g) 7)
If I just run the code, it highlights the error in split-list-tail, where car causes a contract violation. It also shows the call sequence.
That's probably enough to identify the fault, but I can also run the debugger. By clicking on the Debug button I move to the debug mode. By moving the mouse pointer over a parenthesis, and right clicking, I can enable or disable a pause at this point - show by a pink circle. The variables are shown on the right hand side. When I run the code in the debugger, variable accum is empty, hence car accum fails.
My suggestion is that you use pair? or null? to test accum before calling car on it.
Try this. It makes use of three accumulating parameters. A subtraction is also kind of accumulation.
(define (split n xs k)
(define (loop xs n k)
(if
(and (> n 0) (not (null? xs)))
(loop (cdr xs) (- n 1)
(lambda (a b) (k (cons (car xs) a) b)))
(k '() xs)))
(loop xs n k))
Can you see how it is to be called?
Can you explain how it works?
Or a bit shorter,
(define (split n xs k)
(define ((+ f a) b) (f (cons (car a) b)))
(define (loop xs n k)
(if
(and (> n 0) (not (null? xs)))
(loop (cdr xs) (- n 1) (+ k xs))
((k '()) xs)))
(loop xs n k))
Can you find a way how should this function be called? I'll give you a hint, how should this function
(define ((add a) b) (list a b))
be called?

Scheme program to multiply all the elements of a list together

This program is meant to multiply all the elements of an array together in scheme and output the total but so far it has only been returning 0 as the output.
(define (mult-list lst)
(if (null? lst)
0
(* (car lst)
(mult-list (cdr lst)))) )
The problem is that 0 * <anything> is still 0, and the multiplication by 0 gets propagated up the function calls since the last function call will always return 0 (which then gets multiplied by the next number, which is still 0, and the next, still 0, etc).
The solution to this would be to return 1 instead of 0, since 1 is the multiplicative identity the same way 0 is the additive identity. Since anything multiplied by 1 is itself, this will mean that the last item in the list gets multiplied by 1 (still = to the last item) which then gets multiplied by the second-to-last item, etc.
Alternatively, instead of returning 1 when the list is empty, you can return the only item in the list ((car lst)) when the list has 1 item left in it ((null? (cdr lst))).
I'm more a fan of Church numerals, which are, along with arithmetic operations on them, expressed as applications of functions in the elegant and fundamental lambda calculus. Note the absence of *.
(define (mult-list lst)
(letrec ((mul (lambda (m n) (lambda (f) (lambda (x) ((m (n f)) x)))))
(church (lambda (n)
(if (= n 0)
(lambda (f) (lambda (x) x))
(lambda (f) (lambda (x) (f (((church (- n 1)) f) x)))))))
(unchurch (lambda (cn) ((cn (lambda (x) (+ x 1))) 0))))
(let loop ((lst (map church lst))
(acc (church 1)))
(if (null? lst)
(unchurch acc)
(loop (cdr lst) (mul (car lst) acc))))))
(write (mult-list '(2 3 4)) ; 24
You might also be interested in the use of a named let to express recursion instead of calling the top-level function directly. Very useful in more complicated functions.
(define mul1 (lambda (l) (apply * l)))
(define mul2 (lambda (l) (reduce-left * 1 l)))
(define mul3
(lambda (l)
((lambda (s) (s s l))
(lambda (s l)
(or (and (null? l) 1)
(* (car l) (s s (cdr l))))))))
And here I wrote the Peano multiplication that looks more complicated, but in fact it is simpler, as it multiplies by using recursive addition! It uses only the operator SUCC, the predicate EQUALP and the constructor ZERO.
(define mul/peano
(lambda (l)
(define SUCC (lambda (x) (+ x 1)))
(define EQUALP =)
(define ZERO 0)
;; Peano Axioms
(define ZEROP (lambda (x) (EQUALP x ZERO)))
(define ONE (SUCC ZERO))
(define SUB1 (lambda (x)
((lambda (s)
(if (ZEROP x) ZERO (s s ONE)))
(lambda (s x+)
(if (EQUALP x x+)
ZERO
(SUCC (s s (SUCC x+))))))))
(define ADD
(lambda (a b r)
((lambda (s) (s s a r))
(lambda (s a c)
(or (and (ZEROP a) (c b))
(s s (SUB1 a)
(lambda (x)
(c (SUCC x)))))))))
((lambda (s) (s s l (lambda (total) total)))
(lambda (s l ret)
(or (and (null? l) (ret ONE))
(and (ZEROP (car l)) (ret ZERO))
(s s (cons (SUB1 (car l)) (cdr l))
(lambda (r1)
(s s (cdr l)
(lambda (r2)
(ADD r1 r2 ret))))))))))
Note that I defined 0-1=0, as this is how Peano does.
If we're willing to count in unary as in the other answers, we might as well just count in unary -- with length:
(define (mult-list lst)
(length
(crossProduct
(map mkList lst))))
(define (mkList n)
(cond ((> n 0) (cons n (mkList (- n 1))))
(else (list))))
(define (crossProduct xs)
(cond
((null? xs) (list (list)))
(else
(let* ((a (car xs))
(d (cdr xs))
(p (crossProduct d)))
(apply append
(map (lambda (x)
(map (lambda (q) (cons x q))
p))
a))))))
Testing:
> (mult-list '(2 3 4))
24
> (crossProduct (map mkList '(2)))
'((2) (1))
> (crossProduct (map mkList '(2 3)))
'((2 3) (2 2) (2 1) (1 3) (1 2) (1 1))
> (crossProduct (map mkList '(2 0 3)))
'()

Translating Python into Scheme/Racket

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)))]))

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)))

rearrange elements in a List using Scheme

I am trying to write a code using SCHEME that takes two arguments, for example '(2 1 3) & '(a b c) and gives a list '(b a c). My code is not working either recursive or iterative. Any help!!
(define project
(lambda (list1 list2 list3 n b index)
(define n (length(list1)))
(let ((i n))
(for-each (i)
(cond
((null? list1) (display "empty"))
(else
(define n (car list1))
(define index (- n 1))
(define b (list-ref list2 index))
(define list3 (cons list3 b))
(define list1 (cdr list1))
list3 ))))))
(define (rearrange order l)
(cond ((number? order) (rearrange (list order) l))
((list? order) (map (lambda (num) (list-ref l (- num 1))) order))
(else 'bad-order)))
If you need order to be 'complex' (like '(1 (2 3) 4)) then use this:
(define (listify thing)
(cond ((null? thing) '())
((pair? thing) (apply append (map listify thing)))
(else (list thing))))
> (listify 10)
(10)
> (listify '(1 (2 3) 4))
(1 2 3 4)
>
and then
(define (rearrange order l)
(map (lambda (num) (list-ref l (- num 1)))
(listify order)))
Here's a version that handles arbitrarily-nested lists: first, a nested-map that is like map but handles nested lists:
(define (nested-map func tree)
(if (list? tree)
(map (lambda (x)
(nested-map func x))
tree)
(func tree)))
Then, we create a mapper to use with it (using list-ref if the list is shorter than 16 elements, otherwise copying to a vector first for better scalability):
(define (rearrange indices lst)
(define mapper (if (< (length lst) 16)
(lambda (i)
(list-ref lst (- i 1)))
(let ((vec (list->vector lst)))
(lambda (i)
(vector-ref vec (- i 1))))))
(nested-map mapper indices))
Notice how, after the mapper is defined, the function is simply a single call to nested-map. Easy! :-D
First that came to mind:
(define (rearrange order symbols)
(define (element i list)
(if (= i 1)
(car list)
(element (- i 1) (cdr list))))
(define (iter order output)
(if (null? order)
output
(iter (cdr order)
(append output (list (element (car order) symbols))))))
(iter order '()))
Better solution:
(define (rearrange order symbols)
(define (nth-element i list)
(if (= i 1)
(car list)
(nth-element (- i 1) (cdr list))))
(map (lambda (x) (nth-element x symbols)) order))
Here's a simple version for un-nested lists:
(define (arrange idx lst)
(map (lambda (i) (list-ref lst i)) idx))
(arrange '(1 0 2) '(a b c))
=> '(b a c)
If you need to use nested lists, flatten comes in handy:
(define (arrange idx lst)
(map (lambda (i) (list-ref lst i)) (flatten idx)))
(arrange '(1 (0 2)) '(a b c))
=> '(b a c)
Note that I use 0-based indexes, as is the custom in Scheme.