Is it possible to define a function without arguments in a racket?

I am trying to define a function in Racket that takes no arguments. All the examples I've seen take one or more arguments. How can i do this?

+4
source share
3 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 need a procedure that can take any arguments and ignore them, equivalent definitions:

(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"
+7
source

The answer can be found here in HtDP 2e:

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

" , . , , . , ." ( )

: , .

, :

(define (fun) "hello")

:

(define not-a-fun "hello")
+2

You can just say

(define (hello-world)
  (displayln "Hello world"))

(hello-world)
0
source

All Articles