[ home / faq ] [ a / jp ] [ b ] [ aes / fit / g / lit / mu / v ] [ qc ] [ all ] [ irc ]

/g/ - Technology

Name
Email
Subject
Comment
File
Password (For file deletion.)

File: 1637087206354.png (18.45 KB, 1280x904, 160:113, 1280px-Haskell-Logo.svg.png)

 No.73

Learn Haskell anons! One of the best programming languages out there! If you have any questions about Haskell ask here

 No.74

How about starting off with why someone should consider learning it, and what uses it has?

Also, good to see you're alive, it's been a while

 No.76

>>74
Yeahh it's been a while

Anyways, Haskell is a pure functional, statically typed language. Most of the recent research results make their way into Haskell relatively fast. It's a different approach to programming than the traditional methods, but I believe this is going to be the future.
It is a general purpose language despite pushing for academical features, so you can do pretty much anything in it. As for why someone should consider learning it, well I believe that overall, the very expressive type system and type driven programming in general can lead to reduction of many common errors, the functional approach succeeds in reducing the amount of boilerplate one needs to write as well. I don't think I can put it too well into words, so I can demonstrate some examples maybe


fib = 1 : scanl (+) 1 fib

That declares fib, the infinite sequence of fibonacci numbers. This uses lazy evaluation, one of the main features of Haskell. scanl is a function that takes a function, a starting element, and a list, and produces a new list of
[starting_element, f(starting_element, element_of_list1), f(f(starting_element, element_of_list1), element_of_List2)...]
"1 :" part just prepends 1 to a list. The novel concept here is that fib can be defined in terms of itself, because of laziness. So we say that fib has a 1 in the beginning (the 1 : part) and the rest of the list is scanl (+) 1 fib which, as we said, is a list [1, f(1, first_element_of_fib), ...]
But what lazy evaluation accomplishes here is the fact that scanl doesn't need to generate all of the list at once. In fact, lazy evaluation is what allows you to have infinite lists. It only takes what it needs. As we saw, to generate the first element, scanl doesn't need any data, it's just 1. To generate the second element, it only needs to get the first element of fib (given by 1 :), to get the third element it only needs the second and first element of fib (1 : and the last one generated by scanl). You might be starting to see what I mean.

A simpler example would be

take 10 [1..]

[1..] is an infinite list, equal to N. It doesn't actually exist in memory, as Haskell only generates the data you request. By taking 10 elements, Haskell gives you the first 10 elements of this hypothetical infinite list.

Of course, lazy evaluation isn't all of it. Algebraic types are also a very nice thing.

data MyType =
A
| B
| C

Is more or less the same as C enums. But it doesn't stop there.

data MyType =
A Integer
| B Char
| C

Now in your code you can have (A 3), (B 'c'), and just C, and all of them will be of the type MyType while some of them hold additional data, some don't. This pairs up with pattern matching, so you can get.

processMyType :: MyType -> String
processMyType x =
case x of
A somenumber -> "Received " ++ show somenumber
B somechar -> "Received " ++ [somechar]
C -> "Didn't receive anything"


Another thing that's a bit memed are monads which are actually quite nice but this post is becoming long so I'll cut it here, ask if you're interested.

 No.78

File: 1637875356865.png (56.25 KB, 261x243, 29:27, raku-logo.png)

>>73
Fuck Haskell
Use Raku



[Index] [Catalog] [Top] [Post a Reply]
Delete Post [ ]
[ home / faq ] [ a / jp ] [ b ] [ aes / fit / g / lit / mu / v ] [ qc ] [ all ] [ irc ]