(define (fakultaet n)
(if (= n 0)
1
(* n (fakultaet (- n 1)))))
Why does (fakultaet 3)
give me the solution (6) without telling it to print it?
How does it work in DrRacket?
(define (fakultaet n)
(if (= n 0)
1
(* n (fakultaet (- n 1)))))
Why does (fakultaet 3)
give me the solution (6) without telling it to print it?
How does it work in DrRacket?
DrRacket, like many other Scheme programming environments, works by reading each expression you give it, evaluating the current expression to a value (like a number, or a string of characters, or an image), and finally printing out that value to the interaction window before starting the process again on the next expression.
You can test this behavior out for yourself by putting in simpler expressions into the definitions window. For example, just write a number like 103
in there, and hit the Run button; you should see 103
get print out in the interactions window.
In Racket, a function definition ((define (my-function ...) ...)
) is not an expression, so when it gets read and evaluated, it just adds the definition for the new function you have written, but it does not print out any value for it. Instead it just reads the next parenthesized form, adding every definition it sees to its memory and evaluating (and printing the value of) every expression it sees.
In DrRacket it is not one answer to this since it's dependent on which language you are using with DrRacket:
Scheme, a language you get by using #!r5rs
, #!r6rs
, and in the future #!r7rs
does not do this even if you are programming and running it using DrRacket and from the IDE. Often i miss the ability to get top level expressions printed when run from the IDE so much I made a question about how to enable it. Unfortunately it's not possible.
In DrRacket's default language, #lang racket
, every top level expression gets printed when the program runs. Even when compiling the program to an executable and running it outside of racket it does this. In a way this mimics Common Lisp top level as well. Other versions of the racket language, like #lang lazy
and #lang typed/racket
also prints top level expressions. Thus if you make a program that is not supposed to print anything you just make a main procedure that returns (void)
as it's last expression.