Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do 'let' and 'in' mean in Haskell?

I feel like this seemingly simple and essential thing is completely cryptic to me. What does 'let' expression mean? I have tried to google, but the results are full of concepts that I don't understand.

Here is some code I wrote during a lecture. It identifies if a given string is a palindrome. I don't quite understand where the return keyword is either. Is it the 'in'? What is the scope of this function? I am at a loss.

module Main where

isPalindrome :: Text -> Bool
isPalindrome text1 
  = let
    list = toString text1
    backwards = reverse list
    in list == backwards

What does 'in' mean, when it comes after 'let'?

I come from learning C#, and functional programming is unknown to me.

Thank you.

like image 494
CmajorProgram Avatar asked Sep 09 '20 13:09

CmajorProgram


1 Answers

A let-block allows you to create local variables. The general form is

let
  var1 = expression
  var2 = another expression
  var3 = stuff
in
  result expression

The result of this is whatever result expression is, but you can use var1, var2 and var3 inside result expression (and inside expression, another expression, and stuff as well). You can think of let and in as being brackets around the bunch of local variables you want to define. And yes, the thing after in is what you're returning.

like image 72
MathematicalOrchid Avatar answered Sep 22 '22 07:09

MathematicalOrchid