David Contreras
David Contreras

Reputation: 31

Assign multiple variables Haskell

Is there a way to assign the same value to multiple variables in Haskell? e.g something like this:

h,f = 5 

Upvotes: 2

Views: 1115

Answers (1)

wisn
wisn

Reputation: 1024

Prelude> let [x, y] = replicate 2 5
Prelude> x
5
Prelude> y
5
Prelude>

You need replicate to "duplicate" a value. In this case, I duplicating 5 twice. [x, y] mean get x and y from a List. That list is [5, 5]. So, you got x = 5 and y = 5.

Well, I never did such behavior in the real world Haskell but you get what you want.

EDIT: We could use repeat function and the feature of lazy evaluation in the Haskell. Thanks to luqui.

Prelude> let x:y:_ = repeat 5
Prelude> x
5
Prelude> y
5
Prelude>

Upvotes: 5

Related Questions