The program should check if the number 2 belongs to the set A. But the value of x (2) and the set A (1, 2, 4, 5) I want to define in the editor and not in the console. I have this code:
x = 2
let A = [1, 2, 4, 5];
checkIfElem :: Nat -> Bool
checkIfElem x A
|x`elem` A =True
|otherwise = False
It tells me incorrect indentation and I don't know why I just want it to return a true or false, I don't want and don't have to ask for a number or a list.
The contents of variables and lists can be declared inside a .hs file without problems (no error).
In the example I put above it would be:
x = 2
a = [1, 2, 4, 5]
Next, the definition of the function
checkIfElem :: Integer -> [Integer] -> Bool
checkIfElem x (a:xs)
|x`elem` (a:xs)=True
|otherwise=False
The function checkIfElem
receives an integer, a list of integer and returns a boolean value
Now, to call the function using the variable and the list defined in the editor and for Haskell to execute the function with the respective values stored in them, you must write the name of the function and then the variables needed by that function, according to whatever you need such function.
Note: Variables, even if they are lists, are always placed "without further ado", that is, only variables without straight brackets and no other typographical ones.
In Haskell console type:
Prelude> checkIfElem x a
Then, Haskell will answer us True
since the value stored in the variable x
(which is 2) is in the list a
(which is made up of the values 1, 2, 4, 5)