Let's Talk About Functional Programming

Sdílet
Vložit
  • čas přidán 30. 10. 2021
  • References:
    - Source Code: gitlab.com/tsoding/99-ocaml-p...
    - My Old Attempts at Solving These Problems: github.com/rexim/ninety-nine-...
    - My Programming Language (Which is not as cool as OCaml of course): gitlab.com/tsoding/porth
    - rlwrap: github.com/hanslub42/rlwrap
    - Jane Street: www.janestreet.com/
    - Parser Combinators of Monad in OCaml: github.com/tsoding/parcoom
  • Věda a technologie

Komentáře • 120

  • @aradarbel4579
    @aradarbel4579 Před 2 lety +12

    Nice! I've wanted to learn OCaml for a long time now, this is the perfect opportunity to follow along (however slowly) and get some problems solved :D

  • @_vsnwprintf_s_l
    @_vsnwprintf_s_l Před 2 lety +96

    Lisp's main feature is homoiconicity, that being that all code is also data, and therefore lisp has incredibly powerful macros that look just as nice as regular code. I don't know of anything in Python that can achieve anything close to this. The rest of that sentiment was good but I think that's not a good comparison.

    • @dwightk.schrute8696
      @dwightk.schrute8696 Před 2 lety +18

      the problem is that "regular code" in Lisp looks like garbage anyway, hard to screw that up even more than it is

    • @huistelefoon5375
      @huistelefoon5375 Před 10 měsíci +4

      ​@@dwightk.schrute8696what makes you say that?

    • @dwightk.schrute8696
      @dwightk.schrute8696 Před 10 měsíci

      @@huistelefoon5375 ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
      ^^ half of your lisp code

    • @angelcaru
      @angelcaru Před 4 měsíci

      (what
      (makes
      (you
      (say
      (that)))))
      @@huistelefoon5375

    • @insertoyouroemail
      @insertoyouroemail Před měsícem

      @@dwightk.schrute8696 I don't think Lisp code looks like garbage.

  • @rodelias9378
    @rodelias9378 Před 2 lety +5

    Really good one! Thanks a lot and keep doing more stuff like this one please.

  • @MrPetzold123
    @MrPetzold123 Před 9 měsíci +4

    You are an exceptionally good teacher, you actually got me to understand tail call optimization 🤩

  • @jonathanschmider8446
    @jonathanschmider8446 Před 2 lety +48

    Maybe the weird Clang behaviour is because of undefined behaviour? If the function loops infinitely, it causes integer overflow, which is UB. Since the function always loops infinitely, calling it always invokes UB. In case of UB, the compiler can do whatever it wants, and it decided it wants to print garbage.

    • @dnkreative
      @dnkreative Před rokem +3

      Weird clang behavior is because of they completely lost sense while chasing mythical "optimization". This just shows that compiler produces incorrect program. There are many other cases with it (and probably GCC) which even Linus commented.

    • @iuppiterzeus9663
      @iuppiterzeus9663 Před 7 měsíci +1

      not a bad argument

  • @Lars-ce4rd
    @Lars-ce4rd Před 3 měsíci +1

    tsoding: "Let's see if I remember any OCaml from 8 years ago"
    *proceeds explaining me all the syntax of OCaml*

  • @digitalspecter
    @digitalspecter Před 2 lety +41

    Well, yeah, syntax is mostly inconsequential. However, I think with Lisp you're also seeing pretty much the AST which is very handy when writing macros. They can can do whatever manipulations to data/code and spit out code and with Lisp's syntax it's easier to see the structure you're manipulating / trying to achieve. Also Lisp's object model is nicer than Python's (multiple dispatch etc). Also, debugging/modifying a running program is sometimes really nice :) My main objection against Lisp is that it isn't statically typed so the compiler doesn't help me as much as I would like to :)

    • @ribosomerocker
      @ribosomerocker Před 2 lety +4

      Some lisps are statically typed

    • @digitalspecter
      @digitalspecter Před 2 lety +8

      @@ribosomerocker Yup, I meant Common Lisp. I've been following a language called Carp which is quite interesting statically typed compiled language with Rust-inspired memory management.. but it's still quite early in the develpoment.

    • @apestogetherstrong341
      @apestogetherstrong341 Před rokem +2

      Well, static typing would require a static language. Lisp is dynamic. However, with just a few (DECLARE (TYPE ...)) forms, the compiler will not only warn you, but actually refuse to compile when it'll see type conflicts of variables. Common Lisp doesn't lack strict type enforcement, it's just optional.

    • @insertoyouroemail
      @insertoyouroemail Před 7 měsíci +1

      When you have the program running, static analysis is not as important. You're running your code and getting instant feedback anyway. But yeah, sometimes types are nice anyway.

  • @alexanderkhodos
    @alexanderkhodos Před 2 lety +3

    This video also captures a good part of explanations and recipes from HtDP, which even more agrees with Tsoding's rant on syntax/semantics

  • @greob
    @greob Před 2 lety +2

    Super interesting once again!

  • @RicardoValero95
    @RicardoValero95 Před 2 lety +37

    This man should do APL

    • @leastexpected3115
      @leastexpected3115 Před 2 lety +1

      oh, he will love it

    • @BigBeesNase
      @BigBeesNase Před 2 lety +11

      After using APL:
      This man and its viewers looses their sanity.

    • @digitalspecter
      @digitalspecter Před 2 lety +4

      @@rallokkcaz well, you can use FFI to call C-functions.. (at least BQN can). Shouldn't be too hard to open sockets :)

    • @stintaa
      @stintaa Před 11 měsíci

      ew apl

  • @lightningx10
    @lightningx10 Před 2 lety +18

    Loved Haskell, functional programming truly requires you to think outside of the box

    • @leo848
      @leo848 Před 2 lety +9

      Would you think of that as a good thing? I think a language should be as straightforward as possible and not require you to "think outside the box".

    • @Aedaeum
      @Aedaeum Před 2 lety +14

      @@leo848 I guess it depends if thinking outside the box results in more elegant code, or over-engineering...

    • @avananana
      @avananana Před 2 lety +4

      @@leo848 I'd argue it's a good thing, but it has its downsides, the obvious one being that it's not super straight-forward of what to do of course. But the upsides of it is that it forces you to really know what you're doing, which inevitably leads to better and cleaner code if you take coding seriously. There are so many python libraries and projects out there which's source code is pure nonsense because it's so poorly written that it's not even funny as a joke.

    • @CaptainWumbo
      @CaptainWumbo Před 2 lety +7

      it's very useful for solving certain kinds of problems. For me I found that once you start trying to do optimizations to improve performance, you can find yourself bending over backward. Caching is really hard, for example.

    • @superscatboy
      @superscatboy Před 2 lety +4

      I want to make a C compiler that makes you solve a cryptic riddle before it compiles anything. That way I could advertise C as being a language that makes you think outside the box.

  • @blackbeard3449
    @blackbeard3449 Před 6 dny

    1:24:40 clang is so good it calculated the value of infinity!

  • @AdamSchelenbergCom
    @AdamSchelenbergCom Před 2 lety

    Nice clarification on tuples. I was wondering about the semicolon.

  • @flleaf
    @flleaf Před 2 lety +5

    38:20 tsoding is probably on thousands levels of irony

  • @demolazer
    @demolazer Před 2 měsíci

    The best explanation of recursion on the internet.

  • @trabelsiadam3149
    @trabelsiadam3149 Před 2 lety +5

    Need to make a video on how you use the terminal this way 🤤 which multiplexer?.. The hole setup

  • @markblacket8900
    @markblacket8900 Před rokem +2

    the thing with tuples without parentheses is also applicable to python in some cases, so yeah, basically the same language

  • @stanle1101
    @stanle1101 Před 2 lety +5

    Hey nice video, just a note on the stack-pushing behavior of a function call, the first couples of arguments are passed into registers first (regular for int, xmm for float etc.) and if you have extra it'll be pushed onto the stack. In your case I don't think the int x will show up on stack, it'll be passed using the %rcx register :) (the return is given in a register also so no stack space needed)
    So in the end this recursion will just show up on the stack as a bunch of ret addresses, with optimization as you said I guess it'll just be one single ret on the stack. Actually a lot more efficient isn't it?

  • @anilraghu8687
    @anilraghu8687 Před rokem

    Understood the tail call on first reading .😀😃

  • @paulzupan3732
    @paulzupan3732 Před rokem +3

    An interesting thought about the 'rec' keyword in OCaml and why you need to add it to the beginning of recursive function definitions:
    I know that in some functional languages, especially Haskell, the compiled binary of the program includes a runtime which is basically just a lambda calculus interpreter. In the lambda calculus, you can't actually bind functions to names, so that means that before you evaluate an expression, every name needs to be replaced with its corresponding definition before evaluation can begin. There arises a problem when you try to do this with a recursive definition, however, since every time you replace the name of the recursive function with its body, you end up with another copy of it which then needs to be replaced again, etc.
    There is a way to get around it using the Y combinator, but the recursive function needs to take another argument which would stand in for the recursive call in its body.
    In short, the rec keyword may be syntactic sugar for that process. Just a thought though.

  • @benjaminscherrey1124
    @benjaminscherrey1124 Před 2 lety

    I can't find the Tsoding discord channel for some reason. Anyone got an invite link?

  • @antferdom
    @antferdom Před 2 lety +1

    Any thoguths on Coq?

  • @jpr8124
    @jpr8124 Před 2 lety +1

    A good soul who would suggest an OCaml solution to problem 99?

  • @alexloktionoff6833
    @alexloktionoff6833 Před rokem +1

    By what OCaml is better than Rust? generics, matching, optional...
    I see only a more weird syntax of OCaml.

  • @frechjo
    @frechjo Před 2 lety +4

    I found this funny
    > Complains about people who care about syntax, because it's superficial and not important at all in a language.
    > Obviously frustrated with the syntactic choices in OCaml for comments, lists and tuples. ;P
    Who doesn't care at least a little about syntax? It is the interface between the semantics and our reading eyes. It should be as transparent as possible; if you can't avoid thinking about it, it's maybe bad in some way (after you have learned it, of course).
    A couple things about tail call optimizations (TCO) that were not mentioned in the video:
    Tail calls can be optimized also across functions, not only in self recursive ones. In
    f(x)=g(x)
    the call to g is in tail position, so it can be optimized in the same way (the stack frame for f is not needed when g returns).
    Another thing is that TCO can be a problem for analyzing a stack trace, something to take into account. It eliminates part of the stack context, so when you are trying to understand a bug (after the fact), you might not know how you effectively got there.
    BTW, I think mrbotka being a bot is no excuse for it being impolite. Shame on it >:(

    • @TsodingDaily
      @TsodingDaily  Před 2 lety +10

      I usually ignore majority of the comments, but your take regarding my position on the syntax was just beyond imagination stupid so I had to answer.
      > Complains about people who care **ONLY** about syntax
      I'm not saying that there are no problems with syntaxes. My frustration is the people who keep discussing only syntax despite having way more important problems (a.k.a. bikeshedding).

    • @frechjo
      @frechjo Před 2 lety +5

      @@TsodingDaily
      Look, I had no bad intentions with my comment. If it reads like I'm trying to make fun or something, I apologize.
      I think your point about people bike-shedding syntax is sufficiently clear, don't worry (if not from this, at least from other videos for sure).
      I was just putting together two things that you said, because the *contrast* made it funny (which is probably a matter of perspective).

    • @TsodingDaily
      @TsodingDaily  Před 2 lety +9

      @@frechjo Oh, I see! It's hard for me to read the intentions through the text! I'm sorry for my reaction as well. No offense taken either.

  • @enderger5308
    @enderger5308 Před 2 lety +1

    8:30 - The bike shed will be HOT PINK

  • @casey206969
    @casey206969 Před rokem +1

    Funny thing, fairly recently linux added the option to disable #! or make it a module . So in addition to a system that probably won't boot, your ocamlc programs won't run.

  • @ujjawalsinha8968
    @ujjawalsinha8968 Před 2 lety +1

    Non linear curve of difficulty :)

  • @generix1240
    @generix1240 Před rokem +1

    Tsoding : A beginner friendly and fast language doesn't exist. Meanwhile Go :

  • @hendrikd2113
    @hendrikd2113 Před 2 lety +2

    since every list you tested the length functions with was incrementing from 1, the best solution would have been to just reuse the last-element function. Less bloat!

  • @nitair7808
    @nitair7808 Před 2 lety +2

    Why I was thinking he was talking about Apache Camel :D

  • @dmitrypetrov6557
    @dmitrypetrov6557 Před 2 lety

    Why are you explicitly specifying types? Why not left that stuff to the compiler?

  • @Gornius
    @Gornius Před 2 lety +6

    What's this colorscheme? It's beautiful!

  • @AdamWong
    @AdamWong Před 2 lety +6

    My best friend works for Jane street and I work @ another one of the market makers. Lemme assure you that you are smart enough to work work at these companies.
    I think you just don’t want to cause it doesn’t appeal to your creativity 😏

    • @stas4112
      @stas4112 Před rokem +1

      came looking for this comment, if this guy is "too dumb" to work somewhere we are all fucked haha

    • @stas4112
      @stas4112 Před rokem +1

      btw, amazing channel!

    • @AdamWong
      @AdamWong Před rokem

      @@stas4112 thank you!

    • @Lars-ce4rd
      @Lars-ce4rd Před 3 měsíci

      Pretty sure he was joking. Just a weird flex

  • @adicide9070
    @adicide9070 Před 5 měsíci

    ocaml is a beast.

  • @majam1n
    @majam1n Před rokem

    Ocamlator

  • @Rene-tu3fc
    @Rene-tu3fc Před 2 lety +8

    Golang is fast and newbie friendly! though it doesn't have generics, unnullable pointers and lots of useful modern features. But I think sometimes it's easier to read (and code) 50 lines of if/elses than 10 lines of flatmaps juggling monad transformers and converting between different types

    • @ktaleentkekma5777
      @ktaleentkekma5777 Před 2 lety +2

      also, converting between different types? I dont think you really wanna do that in languages like haskell

    • @romankocherezhchenko34
      @romankocherezhchenko34 Před 2 lety +2

      I dont think so, Golang maybe is fast, but not newbie friendly, i bet

    • @john.dough.
      @john.dough. Před 2 lety +6

      It got generics! (finally)

    • @vladlu6362
      @vladlu6362 Před rokem +2

      @@john.dough. sorry for the 8th month too late reply, but "generics" in go is an half-assed implementation of Rust's traits in order to get an half-assed generic interface for functions.

    • @NathanHedglin
      @NathanHedglin Před rokem

      @@vladlu6362 two half asses make a whole ass 😂

  • @berndeckenfels
    @berndeckenfels Před měsícem

    Unshar does that shebang trick, but I think with base64

  • @caiodavi9829
    @caiodavi9829 Před 11 měsíci

    51:20 i am LOLing 😂😂😂

  • @apestogetherstrong341
    @apestogetherstrong341 Před rokem +4

    Baseless claim that Common Lisp has the same semantics as Python.

  • @Simon-xi8tb
    @Simon-xi8tb Před 2 lety +1

    OOP came from a hype. There is a guy on youtube that made several videos titled like "OOP is garbage" and similar LOL

  • @mohnishkumar6847
    @mohnishkumar6847 Před 2 lety +1

    Name of editor?

  • @Mladjooo
    @Mladjooo Před rokem +4

    Has anybody else noticed "Porn folder" at the bottom of his edtior xd

  • @UncleJacki
    @UncleJacki Před 2 lety

    acm t-shirt!

  • @ruslansmirnov9006
    @ruslansmirnov9006 Před 9 měsíci

    7:41 I REMEMBA NINETEEN...

  • @ammarhoussenbay1844
    @ammarhoussenbay1844 Před 2 lety

    Thank you so much you save my exams. 🙂🙏🙏

  • @reisaki18
    @reisaki18 Před 2 lety +3

    APL/DYALOG review

  • @lhpaoletti
    @lhpaoletti Před 5 měsíci

    I never know if Alexey messes up the pronunciation of "raise" on purpose or not, and at this point, I'm too afraid to ask XD

  • @stokedfool
    @stokedfool Před 10 měsíci

    "Common Lisp is straight up Python with s-expressions".
    hot take

  • @mooncorizer290
    @mooncorizer290 Před měsícem

    Bytecode exists in js

  • @Simon-xi8tb
    @Simon-xi8tb Před 2 lety +27

    No, Lisp is not basically python with different syntax. By that logic, every other c -like language is basically Lisp. Lisp has expressions, python has statements. Expressions > statemets, because of better REPL ergonomics. Also Lisp is homoiconic and this allows you to have the best macro system. Also, not all lisps are the same, Clojure is a totally different beast than Python.

    • @vytah
      @vytah Před 2 lety +6

      One imperative, dynamically-typed garbage-collected language with bolted-on functional programming and runtime type information vs another imperative, dynamically-typed garbage-collected language with bolted-on functional programming and runtime type information... A lot of popular languages today are quasi-Lisps - and if you ignore the static vs dynamic typing difference, which only affects how strict your compiler is, then almost all of them are. Code in Common Lisp, Scheme, Python, Java, Ruby, C#, Javascript etc. is pretty similar structure-wise. You have references to heap-allocated objects and you toss them around, imperatively, without worrying about cleaning them up.
      You want something different from all of those Lisps and quasi-Lisps? There's Haskell, C, APL, Forth, COBOL, Erlang.

    • @Simon-xi8tb
      @Simon-xi8tb Před 2 lety +11

      @@vytah Clojure is a functional lisp, it does not have "bolted-on functional programming", it's functional by design. Immutable data structures via persistent collections, support for higher order functions, functions as values... I don't really care for your definition of quasy-lisps. The main difference with lisps and your quasi-lisps is that lisps actually have s-expressions, while your quasi-lisps don't and this is the big difference. This is what is the reason of great repl experience. You can never have as great repl experience using your quasi-lisps.

    • @monad_tcp
      @monad_tcp Před 8 měsíci

      Its the opposite. Python list comprehension is just a bad sort of Lisp

  • @wantharry
    @wantharry Před 2 lety +4

    Hey Buddy, please include screen key , will be helpful as you type commands

  • @zwanzikahatzel9296
    @zwanzikahatzel9296 Před 2 lety +4

    youtuber xah lee would disagree: to him syntax is the most important thing. I've heard that progress in calculus pretty much stalled because of the awkward newtonian notation f' f" etc. until the german started using the df/dx notation, not sure it's true, but syntax does make certain patterns or semantics more visible/exploitable.

  • @Narblo
    @Narblo Před 2 lety +2

    Wait a second. So how old is he???

  • @zwanzikahatzel9296
    @zwanzikahatzel9296 Před 2 lety +2

    another "creative" solution to the length problem is to map all elements of the list to 1 and then sum all the 1s hahah

  • @matthijshebly
    @matthijshebly Před 4 měsíci

    Everyone is pinkt

  • @9SMTM6
    @9SMTM6 Před 2 lety +7

    37:30 Yeah, at this point it's clear to me I do not like OCAML.
    It started to annoy me with the already flipped relation with option, where the type was 'a option and the value Some x, and continued with the traditional parameter definition with Haskell style argument passing, but now this shit with tuples and their types.
    Dont get me wrong, while it's slightly annoying, I tend not to be a person that dislikes a language when it's just different.
    But there's different but of equal sense, like the way you pass arguments in HASKELL, and there's nonsensical different. And, at least from what I see, Ocaml chose the nonsensical route.
    Why would you break the Symmetry between so many elements of the language apparently by purpose? At least at this point I can't see a legitimate reason to do that shit.
    EDIT: Oh, and I could not agree less with you on your comment about Rust. Rust HAS A REASON to be different. Maybe I'm not fair to Ocaml, I'm open for proper explanations, but I do not see it right now.
    EDIT 2: Watching on I see I was wrong about the parameter/argument symmetry break. But continuing to watch Ive yet to see a good reason to end statements with TWO semicolons. Also, how could I forget about that, the head :: rest match, when matching just one element uses [x] and empty [].

    • @csbnikhil
      @csbnikhil Před 2 lety +3

      You can match a one element list with x::[]. And I think the two semi colons are because of it supporting Imperative Programming, where you separate the statements by a single semicolon. Yes, there is a single semi colon in OCaml, and has a different meaning than the double semi colon. You don’t actually need the double semi colons in you source. They’re there to just make the interpreter happy, afaik. And the standards discourage the use of them.

    • @YoTengoUnLCD
      @YoTengoUnLCD Před 2 lety +3

      Ugh, the pattern matching you mentioned works exactly like it does in Haskell, which you supposedly like.

  • @monad_tcp
    @monad_tcp Před 8 měsíci

    I have 99 problems but OCaml aint one

  • @Pompomatic
    @Pompomatic Před měsícem

    mrbotka not obviously a bot lmao

  • @lanceang795
    @lanceang795 Před 2 lety

    Second

  • @neuzen
    @neuzen Před 2 lety

    стопэ, ты может скалу ещё не уважаешь?

  • @simonfarre4907
    @simonfarre4907 Před 2 lety +3

    She bangs are used by all languages that can produce scripts, interpreted or compiled. Python does it, Kotlin does it, D does it. The she bang is just telling the shell what to run it with.

  • @blt_r
    @blt_r Před 2 lety +2

    Can someone explain to me why all of these are basically O(N) solutions of O(1) problems. I get it that optimization is not the main thing here, but if you can do something in constant time why iterate over whole thing? And if performance really doesn't matter at all, why are you trying to make code more performant using tail recursion by sacrificing readability with that function inside a function?
    Why we can't just do something like this to get last element of the list:
    match List.length xs with
    | 0 -> None
    | n -> Some (List.nth xs (n-1))
    knowing lists length and indexing in constant time are very basic things, why someone would even try to solve problems without them?

    • @TsodingDaily
      @TsodingDaily  Před 2 lety +7

      But List.nth is also O(n) github.com/ocaml/ocaml/blob/7997b65fdc87909e83f497da866763174699936e/stdlib/list.ml#L37-L43

    • @blt_r
      @blt_r Před 2 lety +5

      @@TsodingDaily oh, so these lists are linked lists. Therefore you can take head and tail so easily. Now that's makes a lot of sense

  • @bursthooverbag10
    @bursthooverbag10 Před 8 měsíci

    99 problems but Haskel aint one

  • @patryk_49
    @patryk_49 Před 2 lety +7

    Here's the algorithm for solving problems with tail recursion:
    step 1: solve the problem using for loop
    step 2: transform the for loop into recursive function.

    • @patryk_49
      @patryk_49 Před 2 lety +2

      @Mohammed Shahid Tail recursion is when the recursive call is the last instruction of the recursive function.

  • @anilraghu8687
    @anilraghu8687 Před rokem

    Now Tsording stopped using fucking, crap etc.

  • @Dziaji
    @Dziaji Před 2 lety +2

    You did a great job explaining this language. I have never seen it before. Unfortunately, it is the biggest piece of crap I have ever seen. Why would anyone use this when we have the programming masterpiece that is C++ and the high-level, convenient language that is python??