Foldleft in Racket - racket

I have a question regarding trying to define a recursively defined foldl function in Racket.
Here is my approach:
(define foldl
(lambda (z c xs)
(match xs
(empty z)
((make-pair x xs) (foldl c (c z x) xs)))))
Unfortunately, when I do this, I get the Error:
expected a function after the open parenthesis but received 1
I cannot quite figure out why this message is popping up. Can someone help me?

The error you are seeing is use to swapping the arguments to foldl.
Your definition is:
(define foldl
(lambda (z c xs)
Here z is an element a c is a constructor.
In
(foldl c (c z x) xs)))))
you swapped the first two argument.
Note: You need to change the match patterns.
Change empty to '().
Change make-pair to cons.

Related

The Little Typer. I don't understand the meaning of The Initial Second Commandment of λ

I have tried following examples, but no matter y occurred or not,
The function f returns the same value as (λ(y)(f y)) after application.
I would like to do is to define a function that is not the same (-> Y X) as (λ (y)(f y)) when y occurred in y as a counter example, but I don't know how.
Do I misunderstand the meaning of The Initial Second Commandment of λ?
;;y does not occurs
(claim f (-> Nat Nat))
(define f
(λ(y)
0))
;; both return (the Nat 0)
(f 5)
((the (-> Nat Nat)
(λ(y)
(f y)))
5)
;; y occurs
(claim g (-> Nat Nat))
(define g
(λ(y)
y))
;;both return (the Nat 5)
(g 5)
((the (-> Nat Nat)
(λ(y)
(g y)))
5)
In order to create an example illustrating the importance of the caveat "...as long as y does not occur in f", we need to create a function f in which a name y occurs free. The provision that y is free in f is critical. This is also why it is difficult to create such an example: (top-level) functions cannot contain free variables. However, functions that are interior to other functions can. This is the key.
Here is function g that contains another function inside of it:
(claim g (-> Nat
(-> Nat
Nat)))
(define g
(lambda (y)
(lambda (x) ;; Call this inner
y))) ;; function "f"
(I've chosen to write the claim in this way to emphasize that we are thinking about a function inside of a function.)
To get our bearings, this simple function g expects two Nat arguments, and returns the first.
Let's call the inner function f. Note that f contains a free variable y (for this reason, f is meaningless outside of g). Let's substitute (lambda (y) (f y)) for f:
(claim g1 (-> Nat
(-> Nat
Nat)))
(define g1
(lambda (y)
(lambda (y) ;; Here we've replaced "f"
((lambda (x) ;; with an eta-expanded
y) ;; version, introducing
y)))) ;; the name "y"
We can eliminate the application to produce the following expression:
g1
---------------- SAME AS
(lambda (y)
(lambda (y)
((lambda (x)
y)
y)))
---------------- SAME AS
(lambda (y)
(lambda (y)
y))
---------------- SAME AS
(lambda (y)
(lambda (y1)
y1))
In the last step, I've renamed the second y to y1 to illustrate that the variable in the body of the inner function refers to the closer binding site, and not the farther one.
To recap, we started with a function g that "takes two (curried) arguments and returns the first". We then introduced a faulty eta-expansion around the inner function. As a result, we ended up with a function g1 that "takes two (curried) arguments and returns the second". Clearly not equivalent to the original function g.
So this commandment is about variable capture, which is the price we pay for working with names. I hope that helps!
IMPORTANT NOTE:
Due to the way that Pie checks types, you will need to introduce an annotation in the body of g if you want to try this example out:
(claim g1 (-> Nat
(-> Nat
Nat)))
(define g1
(lambda (y)
(lambda (y)
((the (-> Nat Nat)
(lambda (x)
y))
y))))

Reverse list on top level without 'appent' or 'list'

I made some function that can reverse simple lists, like (q w e r t y)
By the task it should correctly process: empty lists, lists, pairs, improper lists.
But now it fails on improper lists, like (q w e r t . y) or pairs.
How to process this situations?
My code:
(define myInverse2
(lambda (original result)
(
cond ((null? original)
result )
(#t
(myInverse2 (cdr original) (cons (car original) result)) )
) ) )
And dr Racket output:
Your code fails because when original is not null?, you assume you can take the cdr of it, which is not always guaranteed. You could fix your code to distinguish between cons? values and other values.
But first, ask yourself if this is necessary and what would be the reverse of some of your inputs. The reverse of a simple pair (x . y) is (y . x).
But what about the reverse of
(q w e r t . y)? I would expect reverse be its own inverse function (i.e. involution), so that you always have:
(equal? x (reverse (reverse x)))
... and if the reverse of the above is (y t r e w q), then you lose this property. Or, you could chose to have the result be (y t r e w . q), which, when reversed, gives you your result back. That is why you first have to chose what is the meaning of your function. Then, if the above approach is the one you want to take, then change should be easy; e.g. add a case in your cond which matches (cons? original).

scheme higher order func

Given the skeleton of a function:
(define gen-hash-division-method (lambda (size)))
as well as:
(define hash-1 (gen-hash-division-method 701))
What I have coded:
(define gen-hash-division-method
(lambda (size)
(lambda (w)
(modulo key(flip(w)) size))))
key(flip(w)) takes a list w and returns an integer.
And call:
(hash-1 '(h e l l o))
I keep getting this error:
procedure application: expected procedure, given: (h e l l o) (no arguments)
You're getting the error because in Scheme (w) expects w to be a function. But w is just a list of symbols.
In your case you have key(flip(w)) which doesn't make sense in Scheme land.
everything is surrounded by parentheses
You want (key (flip w))
Remember the lisp mantra : (function args ...)

Scheme function that sum number u and list x u+x1+x2

Im new to Scheme and trying to make function that is (in f u x), u is integer, x is a list and f binary function. The scheme expression (in + 3 '(1 2 3)) should return 3+1+2+3=9.
I have this but if i do (in + 3 '(1 2)) it return 3 not 6. What am i doing wrong?
(define (in f u x)
(define (h x u)
(if (null? x)
u
(h (cdr x) (f u (car x)))))
(h x 0))
From what I understand of what your in function is supposed to do, you can define it this way:
(define in fold) ; after loading SRFI 1
:-P
(More seriously, you can look at my implementation of fold for some ideas, but you should submit your own version for homework.)

"unfold" for common lisp?

I learned quite a bit of scheme from SICP but am more interested in common lisp now. I know common lisp's fold is reduce, with special arguments for left or right folding, but what is the equivalent of unfold? Googling has not helped much. In fact I get the impression there is no unfold???
Common Lisp has (loop ... collect ...). Compare
(loop for x from 1 to 10 collect (* x x))
with its equivalence using unfold:
(unfold (lambda (x) (> x 10))
(lambda (x) (* x x))
(lambda (x) (+ x 1))
1)
In general, (unfold p f g seed) is basically
(loop for x = seed then (g x) until (p x) collect (f x))
Edit: fix typo
The common lisp hyperspec doesn't define an unfold function, but you can certainly write your own. Its scheme definition translates almost symbol for symbol.