Is this possible to define a function with no arguments in racket? - function

I'm trying to define a function in Racket which takes no arguments. All the examples that I have seen take one or more arguments.
How can I do that?

(define (fun1)
"hello")
(define fun2
(lambda ()
"world"))
(define fun3
(thunk
"I am back"))
(fun1)
=> "hello"
(fun2)
=> "world"
(fun3)
=> "I am back"
EDIT
If, as #Joshua suggests, you want a procedure which can take any argument(s) and ignore them, the equivalent definitions would be:
(define (fun1 . x)
"hello")
(define fun2
(lambda x
"world"))
(define fun3
(thunk*
"I am back"))
(fun1)
(fun1 1 2 3)
=> "hello"
(fun 2)
(fun2 4 5 6 7)
=> "world"
(fun3)
(fun3 8 9)
=> "I am back"

The answer can be found in HtDP 2e here:
http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html#%28part._sec~3afuncs%29
"...Here are some silly examples:
(define (f x) 1)
(define (g x y) (+ 1 1))
(define (h x y z) (+ (* 2 2) 3))"
...then later...
"The examples are silly because the expressions inside the functions do not involve the variables. Since variables are about inputs, not mentioning them in the expressions means that the function’s output is independent of their input. We don’t need to write functions or programs if the output is always the same." (emphasis mine)
That is the answer to your question: you do not need to define no-argument functions, just define them as constants.
So instead of:
(define (fun) "hello")
You just need:
(define not-a-fun "hello")

You can simply say
(define (hello-world)
(displayln "Hello world"))
(hello-world)

Related

Feeding functions as parameters in Scheme

I am new to Scheme programming and working on an assignment. I have multiple functions with func3 sitting at the top and func1 feeding to func2 which then feeds into func3. For example I have something like this
(define func1
(lambda (a b)
(+ a b)))
(define func2
(lambda (x y)
(+ x y))) ;;; y is a function of above function1
I have created func3 as below
(define func3
(lambda (a b x)
func2 (x (func1 (a b)))))
Is there something that I doing grossly wrong or is it something simple? Let me know if any clarifications are needed. Thanks in advance for any help.
The way you're calling the procedures isn't right, this is how it should look like:
(define func3
(lambda (a b x)
(func2 x (func1 a b))))
As you can see, some of the parentheses in your code were misplaced. The key is to understand how to call a procedure, for example this was wrong:
func2(x ...) ; missing `(` at the left
This is also incorrect:
(func1 (a b)) ; don't surround parameters with `()`
The correct way is to surround each procedure call with () but not its parameters, unless they're procedure calls themselves. Like this:
(func1 a b)
(func2 x (func1 a b))

"Adding" two functions together in Scheme

I am going through a practice exam for my programming languages course. One of the problems states:
Define a function named function+ that “adds” two functions together and returns this composition. For example:
((function+ cube double) 3)
should evaluate to 216, assuming reasonable implementations of the functions cube and double.
I am not sure how to approach this problem. I believe you are supposed to use the functionality of lambdas, but I am not entirely sure.
If you need a procedure which allows you two compose to unary procedures (procedure with only 1 parameter), you'll smack yourself in the head after seeing how simple the implementation is
(define (function+ f g)
(λ (x) (f (g x))))
(define (cube x)
(* x x x))
(define (double x)
(+ x x))
((function+ cube double) 3)
;=> 216
Basically if you need to do that you just do (x (y args ...)) so if you need to have a procedure that takes two arguments proc1 and proc2 returns a lambda that takes any number of arguments. You just use apply to give proc1 arguments as a list and pass the result to proc2. It would look something like this:
(define (compose-two proc2 proc1)
(lambda args
...))
The general compose is slightly more complicated as it takes any number of arguments:
#!r6rs
(import (rnrs))
(define my-compose
(let* ((apply-1
(lambda (proc value)
(proc value)))
(gen
(lambda (procs)
(let ((initial (car procs))
(additional (cdr procs)))
(lambda args
(fold-left apply-1
(apply initial args)
additional))))))
(lambda procs
(cond ((null? procs) values)
((null? (cdr procs)) (car procs))
(else (gen (reverse procs)))))))
(define (add1 x) (+ x 1))
((my-compose) 1) ;==> 1
((my-compose +) 1 2 3) ; ==> 6
((my-compose sqrt add1 +) 9 15) ; ==> 5

Lisp Anonymous Function Local Variable

How do I assign anonymous functions to local variables in either cl, emacs lisp or clojure?
I've tried the following with no success.
(let ((y (lambda (x) (* x x)) )) (y 2))
and
((lambda (x) 10) (lambda (y) (* y y)))
In CL, you could use flet or labels.
(defun do-stuff (n)
(flet ((double (x) (* 2 x)))
(double n)))
(do-stuff 123) ;; = 246
As Chris points out, since double is not recursive, we should use flet, as the difference between the two is that labels can handle recursive functions.
Check out docs for info on labels, or this question for the difference between labels and flet.

Creating a Function that produces a Function Scheme/DrRacket

I'm working on a function that takes in a list of structures and then using that list of structures produces a function that processes a list of symbols into a number. Each structure is made up of a symbol, that will be in the second list consumed, and a number. This function produced has to turn the list of symbols into a number by assigning each symbol a value based on the previous structures. Using abstract list functions btw.
Example: ((function (list (make-value 'value1 10) (make-value 'value2 20)))
(list 'value1 'value2 'nothing 'value1)) would produced 40.
Heres my code but it only works for specific cases.
(define (function lst)
(lambda (x) (foldr + 0 (map (lambda (x)
(cond
[(equal? x (value-name(first lst)))(value-value (first lst))]
[else (value-value (second lst))]))
(filter (lambda (x) (member? x (map value-name lst)))x)))))
Looks like a homework. Basic shape of your solution is ok. I think the reason you have a problem here is that there is no decomposition in your code so it's easy to get lost in parentheses.
Let's start with your idea of fold-ing with + over list of integers as a last step of computation.
For this subtask you have:
1) a list of (name, value) pairs
2) a list of names
and you need to get a list of values. Write a separate function which does exactly that and use it. Like this
(define (function lst)
(lambda (x) (foldr +
0
(to-values x lst)))
(define (to-values names names-to-values)
(map (lambda (name)
(to-value name names-to-values))))
(define (to-value n ns-to-vs)
...)
Here we map over the names with another little function. It will lookup the n value in ns-to-vs and return it or 0 if there is no one.
There are two approaches for solving the problem with foldr, it'd be interesting to study and understand both of them. The first one, attempted in the question, is to first produce a list with all the values and let foldr take care of adding them. It can be implemented in a simpler way like this:
(define (function lst)
(lambda (x)
(foldr +
0
(map (lambda (e)
(cond ((assoc e lst) => value-value)
(else 0)))
x))))
Alternatively: maybe using foldr is overkill, applying + is simpler:
(define (function lst)
(lambda (x)
(apply +
(map (lambda (e)
(cond ((assoc e lst) => value-value)
(else 0)))
x))))
In the second approach we take the input list "as is" and let foldr's lambda perform the addition logic. This is more efficient than the first approach using foldr, because there's no need to create an intermediate list - the one generated by map in the first version:
(define (function lst)
(lambda (x)
(foldr (lambda (e a)
(cond ((assoc e lst) => (lambda (p) (+ a (value-value p))))
(else a)))
0
x)))
In both approaches I'm using assoc for finding the element in the list; it's easy to implement as a helper function if you're not allowed to use it or if it doesn't work for the values created with make-value: assoc takes a list of name-value pairs and returns the first pair with the given name. The => syntax of cond passes the pair returned by assoc to a lambda's parameter and executes it.
And because you're using Racket, there's a bit of syntactic sugar that can be used for returning a function from another function, try this equivalent code, for simplicity's sake:
(define ((function lst) x)
(foldr +
0
(map (lambda (e)
(cond ((assoc e lst) => value-value)
(else 0)))
x)))
Or this:
(define ((function lst) x)
(foldr (lambda (e a)
(cond ((assoc e lst) => (lambda (p) (+ a (value-value p))))
(else a)))
0
x))
Anyway, the result is as expected:
((function (list (make-value 'value1 10) (make-value 'value2 20)))
(list 'value1 'value2 'nothing 'value1))
=> 40

How to make a Clojure function take a variable number of parameters?

I'm learning Clojure and I'm trying to define a function that take a variable number of parameters (a variadic function) and sum them up (yep, just like the + procedure). However, I don´t know how to implement such function
Everything I can do is:
(defn sum [n1, n2] (+ n1 n2))
Of course this function takes two parameteres and two parameters only. Please teach me how to make it accept (and process) an undefined number of parameters.
In general, non-commutative case you can use apply:
(defn sum [& args] (apply + args))
Since addition is commutative, something like this should work too:
(defn sum [& args] (reduce + args))
& causes args to be bound to the remainder of the argument list (in this case the whole list, as there's nothing to the left of &).
Obviously defining sum like that doesn't make sense, since instead of:
(sum a b c d e ...)
you can just write:
(+ a b c d e ....)
Yehoanathan mentions arity overloading but does not provide a direct example. Here's what he's talking about:
(defn special-sum
([] (+ 10 10))
([x] (+ 10 x))
([x y] (+ x y)))
(special-sum) => 20
(special-sum 50) => 60
(special-sum 50 25) => 75
(defn my-sum
([] 0) ; no parameter
([x] x) ; one parameter
([x y] (+ x y)) ; two parameters
([x y & more] ; more than two parameters
(reduce + (my-sum x y) more))
)
defn is a macro that makes defining functions a little simpler.
Clojure supports arity overloading in a single function object,
self-reference, and variable-arity functions using &
From http://clojure.org/functional_programming
(defn sum [& args]
(print "sum of" args ":" (apply + args)))
This takes any number of arguments and add them up.