How to declare variables in Haskell?

Member

by sabryna , in category: Other , 2 years ago

How to declare variables in Haskell?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@sabryna You can declare it with variable name and value in Haskell, see example below:


1
2
3
4
str = "test"

-- Output: test
main = print str


Member

by kenton , a year ago

@sabryna 

In Haskell, variables are declared using the "let" keyword or by defining functions.

  1. Using "let" keyword:


Syntax:

1
let <variable_name> = <value>


Example:

1
let x = 5


  1. Defining functions:


Syntax:

1
<function_name> <parameter_list> = <function_body>


Example:

1
add a b = a + b


In Haskell, variables are immutable, which means that you cannot change their values once they have been assigned. Therefore, it is often more appropriate to think of them as bindings, rather than variables.