Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 202
Elik Avatar asked Nov 19 '13 20:11

Elik


People also ask

Can a function have no arguments?

A function with argument, as in function(something) is to return a value after processing within the function. A function with no argument, eg function(), is to perform a task with no value returned.

What is a function with no arguments called?

A nullary or niladic function.

Can we call a function without passing arguments?

So, if we call the function without passing the arguments for them, it will throw an error. For the optional parameters, we don't have to pass any arguments. If we don't, the default value will be used.

Do all functions need arguments?

You can define a function that doesn't take any arguments, but the parentheses are still required. Both a function definition and a function call must always include parentheses, even if they're empty.


1 Answers

(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"
like image 139
uselpa Avatar answered Nov 08 '22 15:11

uselpa