Discuss Scratch

W1-F1
Scratcher
100+ posts

Bella ⌁ A simple functional programming language ⌁ Written in Gleam!

I have a question about the new input function of bella;

If I enter left, It shows the message for the option left.

But if I enter right, I need to enter right twice before it shows the message for the option right.

It also does it when you enter an invalid option (else).

Is that because bella first reads If first, then Else if and then Else, but still showing the input of if when the code ‘jumps’ to else if?

It also does it when you enter an invalid option (else).

Is this a issue in my code or is that how the input function works?

The code:
Io.println("Hello, " + Io.readline("What is your name?"))
Io.println("You need to find your way home, you can go left or right.")
if (Io.readline("Enter your option:") == "left")
Io.println("You fell and broke your leg. Game over.")

else if (Io.readline("Enter your option:") == "right")
Io.println("You made it home! Congrats!")
else
Io.println("Please enter a valid option!")

Video
W1-F1
Scratcher
100+ posts

Bella ⌁ A simple functional programming language ⌁ Written in Gleam!

NFlex23 wrote:

W1-F1 wrote:

My text adventure game! Took me 10 minutes to make.

Io.println("Hello, " + Io.readline("What is your name?"))
Io.println("You need to find your way home, you can go left or right.")
if (Io.readline("Enter your option:") == "left")
Io.println("You fell and broke your leg. Game over.")

else if (Io.readline("Enter your option:") == "right")
Io.println("You made it home! Congrats!")
else (
Io.println("Please enter a valid option!")
)
Very nice!

As a side note, you don't need the parenthesis after the final `else`.
Thank you!
PlNG_
Scratcher
500+ posts

Bella ⌁ A simple functional programming language ⌁ Written in Gleam!

W1-F1 wrote:

I have a question about the new input function of bella;

If I enter left, It shows the message for the option left.

But if I enter right, I need to enter right twice before it shows the message for the option right.

It also does it when you enter an invalid option (else).

Is that because bella first reads If first, then Else if and then Else, but still showing the input of if when the code ‘jumps’ to else if?

It also does it when you enter an invalid option (else).

Is this a issue in my code or is that how the input function works?

The code:
Io.println("Hello, " + Io.readline("What is your name?"))
Io.println("You need to find your way home, you can go left or right.")
if (Io.readline("Enter your option:") == "left")
Io.println("You fell and broke your leg. Game over.")

else if (Io.readline("Enter your option:") == "right")
Io.println("You made it home! Congrats!")
else
Io.println("Please enter a valid option!")

Video

Try letting a variable to the input:
Io.println("Hello, " + Io.readline("What is your name?"))
Io.println("You need to find your way home, you can go left or right.")
let input = Io.readline("Enter your option:") in

if (input == "left")
Io.println("You fell and broke your leg. Game over.")
else if (input == "right")
Io.println("You made it home! Congrats!")
else
Io.println("Please enter a valid option!")
W1-F1
Scratcher
100+ posts

Bella ⌁ A simple functional programming language ⌁ Written in Gleam!

PlNG_ wrote:

W1-F1 wrote:

I have a question about the new input function of bella;

If I enter left, It shows the message for the option left.

But if I enter right, I need to enter right twice before it shows the message for the option right.

It also does it when you enter an invalid option (else).

Is that because bella first reads If first, then Else if and then Else, but still showing the input of if when the code ‘jumps’ to else if?

It also does it when you enter an invalid option (else).

Is this a issue in my code or is that how the input function works?

The code:
Io.println("Hello, " + Io.readline("What is your name?"))
Io.println("You need to find your way home, you can go left or right.")
if (Io.readline("Enter your option:") == "left")
Io.println("You fell and broke your leg. Game over.")

else if (Io.readline("Enter your option:") == "right")
Io.println("You made it home! Congrats!")
else
Io.println("Please enter a valid option!")

Video

Try letting a variable to the input:
Io.println("Hello, " + Io.readline("What is your name?"))
Io.println("You need to find your way home, you can go left or right.")
let input = Io.readline("Enter your option:") in

if (input == "left")
Io.println("You fell and broke your leg. Game over.")
else if (input == "right")
Io.println("You made it home! Congrats!")
else
Io.println("Please enter a valid option!")
That worked! Thanks!
NFlex23
Scratcher
1000+ posts

Bella ⌁ A simple functional programming language ⌁ Written in Gleam!

As I have alluded to a few times before, pattern matching has always been a goal of mine. Well, I've been working on it for a while now and I think it is almost ready to be merged! Please let me know what you think.

This is the PR: https://github.com/MystPi/bella/pull/8

Here is a little example of pattern matching (notice the `match` and `let` expressions):
let
mylist =
[1, 2, 3, 4, 5, 6, 7, 8, 9]

map =
list -> fn ->
match list
is [] then []
is [first | rest] then
[fn(first)] + map(rest, fn)

reduce =
list -> acc -> fn ->
match list
is [] then acc
is [first | rest] then
reduce(rest, fn(first, acc), fn)

fold =
list -> fn ->
let [first | rest] = list in
reduce(rest, first, fn)
in
(
; Squares
map(mylist, x -> x * x) |> Io.println

; Sum
fold(mylist, x -> a -> x + a) |> Io.println

; Length
reduce(mylist, 0, x -> a -> a + 1) |> Io.println
)

And a “kitchen sink”:
let
test = [1, 2, 3]
[a, b, _] = test
^test = [1, 2, 3]
y = 42
42 = y
x = false
false = x
["add", a, b] = ["add", 50, 40]
[x, ^Io.println(x * x), ^Io.println(x * x * x)] = [2, 4, 8]
{ name, cool: true } = { name: "MystPi", cool: true }
{ type: "dog", props: { name } } = { type: "dog", props: { name: "Bella" }}
^{ foo: true, bar: "blah" } = { foo: true, bar: "blah" }
^[1, 1 + 1, 1 + 1 + 1, 1 + 1 + 1 + 1] = [1, 2, 3, 4]
[x, y | rest] = [1, 2, 3, 4]
[x, ^(x + 1) as y, ^(x + 2) as z] = [1, 2, 3]
?Types.is_type("string") = "32"
in

Io.println("Everything matched successfully!")

Last edited by NFlex23 (Dec. 24, 2023 13:52:50)

W1-F1
Scratcher
100+ posts

Bella ⌁ A simple functional programming language ⌁ Written in Gleam!

NFlex23 wrote:

~snip~
Nice!

Powered by DjangoBB