10
zetef
5y

!rant

so the other day i was programming and suddenly i wanted to learn haskell. (i don't know why it hit me so suddenly, maybe because it's a 'pure' functional programming language and these 2 terms i knew nothing about)

and to be honest it's really hard coming from an imperative programming language (C/C++, yes, i know they are different in their ways). it's like learning to program again! you really have to get a different mindset and for me honestly it's hard to grasp the idea that 'variables' are immutable! like, that's soooo weird it still stucks to me. for example how did they define the max or min function without using a while loop? what are monads?

I am just 2 days in but it'll be a fun ride!

Comments
  • 3
    Variables may be immutable, but you can always do something with a variable and put the result into a new variable.

    Anyway, it's not obvious whether immutability is a good thing. Yes, concurrency becomes easy, but at the price of dealing with possibly stale data.
  • 0
    My advice, instead of learning FP + a new language, try to implement FP-techniques in a language you know.
  • 4
    Haskell!

    I kind of like it.
    I kind of hate it.
  • 2
    Immutability is one of those things that, when you start using it in non-functional languages a lot of things gets a lot nicer.

    A feature that I really love(d) about using F# is how null doesn't exist. What you have instead is something called 'None' which you have to be extremely explicit about. Like you can't just declare a string and set it to be none, you have to declare a string that can be either a string or none. What then happens every time you use that string the compiler will yell at you every time you use that string without checking for none.
  • 2
    @Fast-Nop Haskell isn't immutable-only, it allows safe mutation through the ST monad.

    ST's design is pretty neat, it uses the typesystem to ensure that stuff doesn't leak outside the controlled ST environment. Sadly mutation can't always be avoided because persistent data structures and functional patterns aren't always the most efficient ways to solve problems.

    You can also do unsafe operations which break Haskell's guarantees, and there's always the (very simple to use) C FFI for really hairy situations.

    @zetef think of the "variables" as the ones from mathematics - if I say X is 5, how can it become 6 later on? Or think of them "values" instead, or maybe even names for things ("X" is another name for "5" etc.), instead of the "box in which you put some value" interpretation that your traditional programming variables follows.
  • 0
    @zetef Just switch your mindset for Haskell from other non FP languages. It will help a lot.

    Other languages: I am writing how things work line by line.

    Haskell: I am defining what things are.

    Also every loop becomes a recursive call in Haskell :)
Add Comment