Scheme Procedure Repeatedly Apply [closed] - scheme

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I want to create a scheme procedure such as;
(repeatedly-apply p n) of type (A->A),number->(A->A)
It returns a procedure that takes a single argument and applies p to it n times
for example;
((repeatedly-apply inc 10) 2) => 12
Extra Informations:
There is a function that defined and we can use it if we want
(define compose (lambda (f g) (lambda (x) (f (g x)))))
And we can use the base case as (lambda (x) x)

To create a function which repeatedly applies f n times:
if n < 1 this is an error;
if n = 1 the answer is f;
otherwise compose f with a function which repeatedly applies f n - 1 times.
You owe the usenet oracle four magic lantern slides and the colour blue.

Related

add support to new expression scheme language [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm new here. I'm soon to have a test, a test language scheme, and I'm getting ready for it.
I have a question that I try to solve for a few hours but do not even understand how to start it.
The question goes like this:
There is a new expression of this form:
(get-procedure-body <exp>)
This expression receives a single expression as an argument, evaluates it, and if a user procedure type value is obtained, returns
Her body.
Question in scheme language
Hello everyone, I'm new here. I have a test soon, a test language test, and I'm getting ready for it.
I have a question that I try to solve for a few hours but do not even understand how to start it.
The question goes like this:
There is a new expression of this form:
This expression receives a single expression as an argument, evaluates it, and if a user procedure type value is obtained, returns
Her body.
Here are the possible cases after the revaluation of :
If the returned value is a user procedure - its body must be returned
If the returned value is a primitive procedure - “Hidden Implementation! primitive“
If the value is not a procedure, print "procedure-non: error"
Possible output of the operation of the function:
(get-procedure-body f)
> ((display x) (* x y))
(get-procedure-body (lambda (x) (h x)) )
> ((h x))
(get-procedure-body +)
> “primitive! Implementation hidden”
(get-procedure-body 1)
> “error: non-procedure”
(get-procedure-body (+ 1 1))
> “error: non-procedure”
The question goes like this:
Add support for body-procedure-get expressions as a kernel expression (write all required changes)
I would be happy if someone could at least help me, I tried to explain myself, if something is not clear, tell me, I will explain more. This is difficult language for me, especially since it does not have much support.
I could not even figure out what to do.
Getting the body of a procedure is not part of Scheme and not Common Lisp standard, however if you have made a Scheme interpreter (eval) with support for user procedures you most likely have the code in question in your chosen data structure and making the non standard primitive get-procedure-body would be exposing the same procedure from the host language. Eg. The book SICP does have something named very similar as part of their interpreter.
As for using Racket I once made this to be able to do something similar:
#lang racket
(struct proc (src obj)
#:property prop:procedure
(struct-field-index obj)
#:transparent
#:methods gen:custom-write
[(define (write-proc x port mode)
((case mode
[(#t) write]
[(#f) display]
[else pretty-print])
(proc-src x)
port))])
(define-syntax lambda*
(syntax-rules ()
((_ . rest)
(proc '(lambda* . rest) (lambda . rest)))))
(define test (lambda* (x y) (+ x y)))
test ; ==> #(struct:closure (lambda* (x y) (+ x y)) #<procedure>)
(proc-src test) ; ==> (lambda* (x y) (+ x y))
(proc-obj test) ; ==> #<procedure>
((proc-obj test) 1 2) ; ==> 3
(test 1 2) ; ==> 3
Now this solution turns the problem upside down. Since I don't have a way to get the source from a system I make syntax that stores the source alongside the procedure object and use a struct feature to package those. As a procedure using lambda* would work the same as lambda with the exception of introspection.

List the cartesian product of the list with itself [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
(defn slist-cartesian [lst]
(slist-iter
lst
(fn [x]
(slist-iter
lst
(fn [y]
(println x y))))))
So i have an entire code to explain but this part doesn't make sense to me, can someone please explain it to me ? I know list-iter is used when you'd like to see something done with each element of the list but i have trouble following the path here.
Let's assume that slist-iter does what you explained as list-iter behaviour, which is "'used' when you'd like to see something done with each element of the list"...
Now, I'm going to present you another way to do the cartesian product using Clojure core functions, so I can point you the key element that could be confusing you...
(defn slist-cartesian [lst]
(doseq [x lst
y lst]
(println x y)))
That is what doseq does. You can read it something like for each x in lst, and for each y in lst, print x and y.
Now, the key part here is that you are repeating lst both for x and y. That way we get the Cartesian product of lst with lst.
That is exactly what the code you show is doing. It is using lst as the list to be processed by both of the slist-iter calls.
Understand that it is needed to process the list each time from the beginning, to get what you are asking: The Cartesian product.
And now understand that you can do it both with the doseq expression and within the slist-iter functions.
Probably you need to understand why you can do it in the slist-iter form.
It is possible because fn creates a function and also that function is a Closure "which means that 'it' can access all the variables that were in scope when the function was created".
That means that that function slist-cartesian is creating with fn when calling the first slist-iter, has access to the parameter lst. Let's refer to that function as the anonymous one.
That anonymous function will be called again each time the first slist-iter begins to process a new element of lst, but lst is going to be the same slist-cartesian received and used when the later created the former.
I hope this answered your question.
If you still want to think and dig more in the subject, there is another way to think about this is by constructing the function one step at a time.
So in your own words ...
(defn slist-cartesian [lst]
(slist-iter
lst ;; The list with the elements
,,, ;; The thing to be done to the elements to the list
))
The thing to be done is a function that receive 1 parameter. That function will be called passing as an argument an element of the list, one element at a time for all the elements in the list.
If we make println the function, it will call it many times, one per element of the list, with such element as argument. Which means that it would print the whole list, one item at a time:
(defn slist-cartesian [lst]
(slist-iter
lst
println
))
(slist-cartesian '(1 2 3))
;; 1
;; 2
;; 3
What happens when you need to add more information to what is printed? You need a more complex function than simply println.
Let's create a function that will prefix a text to each item on the list and print them. Let's prefix the text "number: ". Because we have slist-iter we just only need to solve the problem of prefixing the text to a single number, with a function with only one parameter so it can be used by slist-iter and so solving the problem for all elements in the list. Let's make that solution a function:
(defn prefix-text-number-and-print [x]
(println "number: " x))
And now let's use it to build a function that prefix "number: " to all elements of a list and print them. Let's call that function prefix-text-number-to-each:
(defn prefix-text-number-to-each [lst]
(slist-iter
lst
prefix-text-number-and-print))
(prefix-text-number-to-each '(1 2 3))
;; number: 1
;; number: 2
;; number: 3
Cool. But what happen if we need to change the text of the prefix? That means that the function prefix-text-number-and-print needs to be generalized by adding a parameter for such prefix. But that will make that function useless for slist-iter. What then?
What we will do is to create the generalized function, and then somehow derive from it an specialized function we need on the precise moment.
(defn prefix-text-and-print [prefix, x] ;; Generalized function
(println prefix x))
So for the soon to be function prefix-text-to-each, we would do something like:
(defn prefix-text-to-each [prefix, x]
(slist-iter
lst
(somehow-derive-a-function prefix-text-and-print prefix))) ;; ATTENTION
There are many ways to create functions on the spot. You already used one tool for that in the code you show. You are using the function fn to build a function in place. Another way to do it is with the function partial. Let's explore the partial option first.
For that, I will do to prefix-text-and-print the opposite of what I did to prefix-text-number-and-print. I will specialize it. I will define prefix-text-number-and-print in terms of prefix-text-and-print. First by hand, and then by using partial:
;; By hand
(defn prefix-text-number-and-print [x]
(prefix-text-and-print "number: " x))
;; Using partial
(def prefix-text-number-and-print
(partial prefix-text-and-print "number: "))
Both definitions produce the same result: a function. Note the last one uses def instead of defn.
The definition using partial is simple a def. So it is simply defining prefix-text-number-and-print with whatever value is created by the call to partial.
partial creates a function. That new function will simply call the first parameter received by partial (in this case prefix-text-and-print) with the rest of the arguments partial received as arguments (in this case only "number: ") followed by the arguments the function itself receives.
Try it, it doesn't matter what version you used, the result will be the same:
(prefix-text-number-and-print 123)
;; number: 123
You could have done it in place of the call, with the same result
((partial prefix-text-and-print "number: ") 123)
;; number: 123
Now that you now how to use partial, let's use it to build a generalized function prefix-text-to-each
(defn prefix-text-to-each [prefix, lst]
(slist-iter
lst
(partial prefix-text-and-print prefix))) ;; ATTENTION
Let's try it:
(prefix-text-to-each "hola: " '(1 2 3)
;; hola: 1
;; hola: 2
;; hola: 3
Now we could think on making a function that combines not a text, but a list, with each element of another list. Again, we begin with combining the thing (a list) to a single element:
(def sufix-list-and-print [a-lst x] ;; Yeap, for practical reasons, let's suffix the thing (the list) instead of prefixing.
(slist-iter
a-lst
(partial prefix-text-and-print x ;; we are reusing this function, why not?
)))
(sufix-list-and-print '(1 2 3) 88)
;; 88 1
;; 88 2
;; 88 3
With that function, we can now create the function that combines 2 lists:
(def print-2-lsts-combined [a-lst b-lst]
(slist-iter
b-lst
(partial prefix-list-and-print a-lst)))
Let's try that:
(print-2-lsts-combined '(1 2 3) '(10 20 30))
;; 1 10
;; 1 20
;; 1 30
;; 2 10
;; 2 20
;; 2 30
;; 3 10
;; 3 20
;; 3 30
It happens that if the 2 lists are the same, you will have the Cartesian product. So you can define the Cartesian product in terms of this function:
(defn slist-cartesian [lst]
(print-2-lst-combined lst lst))
Or by itself in a similar way to the print-2-lst-combined function, which resembles the code you show originally:
(def slist-cartesian [lst]
(slist-iter
lst
(partial prefix-list-and-print lst) ;; ATENTION
))
Now, the original code uses fn instead of partial. fn and partial have the same job: to create a function.
Do you see it?
The problem in the list commented with ATTENTION was to give somehow the list lst to a function that receives only 1 parameter and you couldn't use that parameter for such purpose.
So the solution with partial was to derive such function.
The solution with fn was to express that function in place, and "say" the word lst when needed, because it could due to it being a Closure, as explained before. That is an anonymous function and I invite you to read more about it.

Could anyone give a human language example which could test ∀x.(px=>q) does euqal to ∃x.px=>q [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have problem to understand the equivalence of the following sentence intuitively for long time
∀x.(px=>q)
∃x.px=>q
I understand the truth table below does says they are equivalent:
p(a) p(B) q p(a)=>q p(b)=>q (p(a)=>q)&(p(b)=>q) p(a)|p(b) p(a)|p(b) =>q
T F T T T T T T
F T T T T T T T
T T T T T T T T
F F T T T T F T
T F F F T F T F
F T F T F F T F
T T F F F F T F
F F F T T T F T
But what i'm looking for is an human language example to verify the validity of the equivalence, so I could understand more intuitively, could anyone give a example?
The second one means "there is a px value which implies a q result".
The first one means "for each px value, there is a q result"
This could be the difference between "a driver has a driving licence" and "every drivers must have one".

Algorithm evaluating user-defined functions

Hello I have some homework that consists of extending a lisp interpreter. We are to build three primitives with pre-evaluated arguments ( for exemple <= ), and three primitives who do their own evaluation ( for example if ).
I went beyond the call of duty and created the only fun function in the bounds of this exercice : (defun) [it's the common lisp keyword for defining a user-function].
I would like to know if my algorithm for managing a user-defined function call is worthwhile.
In pseudo code, here it goes :
get list of parameters # (x y z)
get list of arguments # (1 2 3)
get body of function # (+ x (* y z))
for each parameter, arg # x
body = replace(parameter, argument, body) # (+ 1 (* y z))
# (+ 1 (* 2 z))
# (+ 1 (* 2 3))
eval(body) # 7
Are there better ways to accomplish this?
Thanks.
EDIT: replace() is a function recursing on sub-lists of body.
I never found better, no one proposed better, the question generated no interest whatever, and I'm on a rampage to close my opened questions, so here is the answer :
my algorithm was good enough.

Scheme : using lambda as a parameter

hy everyone, for school i have to make a function where lambda is used as a parameter
like so : (string (lambda ...) 5 40) where we have to fill in the dots
this is the function we had to reinvent, the regular string version
(define (string decoration n r) >string decoration is a function that creates a string with either fish or pumpkins hanging on the string
(define (decorations k) >decorations is the recursive function which hangs all the decorations together
(if (= k 1)
(decoration r 10) > here decoration is to be replaced with either a pumpkin or a fish as stated in the parameters
(ht-append (decoration r 10) > ht-append is a function that appends 2 figures Horizontally at the Top
(decorations (- k 1)))))
(hang-by-thread (decorations n))) > hang by thread is a function that hangs all the decorations at a string
all the names should be self-explanatory, the function takes a decoration , either a fish or a pumpkin and hangs it by a thread. But the fish has 3 parameters and the pumpkin has 2 which caused an error. So in a previous exercise we had to make an extra definition called fish-square which uses only 2 parameters to make a fish. Now we have to implement this same squared fish but with a lambda. Any help is greatly appreciated
(define (fish-square wh l) > this is the fish square functio which makes a fish but with 2 times the same parameter so it looks like a square
(vc-append (filled-rectangle 2 l) (fish wh wh))) > the l is the length of the string that attaches the fish to the string at the top
the fish function is just (fish x y) x makes it longer, y makes it taller.
the pumpkin function is just (pumpkin x y) same story
so my question is, how do rewrite the given code , but with lambda as a parameter.
i would upload an image, but my repuation isn't high enough :s
The string procedure as it is already receiving a procedure as a parameter (you don't have to rewrite it!), decoration can be any two-argument function used for decorating. Now when you call it you can pass a named procedure, for example:
(define (decoration r n)
<body>)
(string decoration
5
40)
... Or just as easily, you can pass the same procedure in-line as a lambda, and if I understood correctly, this is what you're supposed to do:
(string (lambda (r n)
<body>)
5
40)
Just replace <body> with the actual body of the decoration you want to use. In othre words: the change you're expected to do is in the way you pass the parameters to the function at invocation time, but you're not expected to change the function itself.
Imagine you have the procedure +. It could be any really. It takes several arguments but you need a different procedure that takes one and adds that to an already constant value 3.
Thus you want to pass + with the extra information that it should add 3.
A full definition of such procedure would be
(define (add3 n)
(+ 3 n))
which is the short form of the full define
(define add3
(lambda (n)
(+ 3 n)))
Now when passing a procedure 3+ you could actually just pass it's definition. These two does the same:
(do-computation add3 data)
(do-computation (lambda (n) (+ 3 n)) data)

Resources