[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

/fpt/ - Functional Programming Thread

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 328
Thread images: 28

File: 1482055600767.png (818KB, 1280x719px) Image search: [Google]
1482055600767.png
818KB, 1280x719px
Next-level programming thread.
They say, to become fluent in a foreign language, one must stay in that country.
Stay in the thread, become fluent in functional programming!
Last one: >>58141396

Resources:
>Elixir
http://chimera.labs.oreilly.com/books/1234000001642/index.html
http://elixir-lang.org/learning.html
>Elm
https://guide.elm-lang.org/
>Erlang
http://learnyousomeerlang.com/content
>F#
http://fsharp.org/learn
>Haskell
https://en.wikibooks.org/wiki/Haskell
>Lisps
>>Common Lisp
http://gigamonkeys.com/book
Common Lisp Recipes
Land of Lisp
An Introduction to Functional Programming Through Lambda Calculus
>>Clojure
http://www.braveclojure.com/foreword/
>>Scheme
SICP
Essentials of Programming Languages
Art of the Propagator
Little Schemer
The Seasoned Schemer
The Scheme Programming Language by Kent Dybvig
Realm of Racket
Lisp in Small Pieces
>OCaml
https://realworldocaml.org/

Feel free to add more.
>>
File: haskell anime.png (368KB, 498x707px) Image search: [Google]
haskell anime.png
368KB, 498x707px
Thank you for using an anime image!
>>
Is the book in the OP better than learn you a haskell?
>>
>>58174068
Probably. LYAH is a bit out of date.
>>
No love for Scala?

Good Scala books:
Functional Programming in Scala (Chiusano and Bjarnason)
Atomic Scala (Eckel and Marsh)
Programming Scala (Wampler and Payne)
Programming in Scala (Odersky, Spoon and Venners)

Also, The Joy of Clojure is another very good Clojure book.
>>
>>58174116
I'll be adding those.
>>
>>58174116
>No love for Scala?
Yes, exactly
>>
So im reading through haskell book in OP and its pretty good. Are there any tutorials/books/youtube vids where someone makes something useful with haskell? For e.g if i were to learn c# i could just have fun by making keyloggers or whatever, and find bunch of youtube vids, on the other hand haskell vids seem to be just recordings of some talks and pretty abstract. I dont mind learning abstract its just i'd like to see concrete, practical examples
>>
File: 1450379066800.png (24KB, 189x189px) Image search: [Google]
1450379066800.png
24KB, 189x189px
>>58164015
I hope you are still here, but the biggest difference between functional and imperative is that imperative is focused on assigning values to things, and these assignments change how your program works. Functional on the other hand, is focused on doing as many things as humanly possible while changing as little as possible.

The idea is that it is easier to reason about a system that doesn't rely on pieces of data that anything could have written to. This is because there are a very large number of possible combinations you need to consider if you do. Sometimes unexpected combinations of these values (called "state") can cause subtle bugs you don't realize, and can sometimes crop up in completely unrelated parts of the system and cause some serious headaches later.

Functional tries really hard to limit the state you need to a bare minimum.

However, generally speaking, imperative code is faster than functional code. This means historically the norm in game development is imperative, because highly-optimized engine code and graphics programming can be more optimized with it. However, this is not a rule--I think a good thing to do is to use the tools that you like the most, because game development is faaaar from a short process, and you don't want to lose motivation partway through.

So, I suggest you should use the things you like using the most.
>>
>>58175772
So, for imperative, pros:
- It's faster
- Easy to reason with on a small scale (This is why you should keep your inheritance shallow)
- Amazingly easy to model real-life things with imperative styles
- Tools for working with these languages often border on wizardry

Imperative cons:
- Holy-shit levels of state permutations to account for
- The wrong state in the wrong place hurts unexpected amounts
- Very hard to test in comparison to functional code
- All of this means technical debt is far easier to accrue, be careful!

For functional, pros:
- Ridiculously easy to test
- Many kinds of bugs become impossible naturally
- You can experiment/play with your code in much more useful/fun ways
- Abstraction is far more reliable, particularly as you layer it

And functional cons:
- Tools for these languages are usually harder to get used to
- You have to separate code that changes anything from everything else
- Harder to organize
- Not used nearly as often as imperative

For recommendations, I would say to pick up on an imperative language first, and then move to a functional one. That way you can better understand what state actually means to you, and get acquainted with all the neat tools and ways of solving problems that both sides use.
>>
>>58175788
For the two languages, I would say that Java and Clojure make a good pair. As a web developer and aspiring game developer, you can seriously benefit from both languages. Here is my reasoning for the two:

For Java:
- There is lots of demand for Java in web development, so it is worth it even if you get nothing else out of it
- It shows you Class-based Object Oriented
- It gives you a tremendous head start for learning C++, the industry standard for game development
- There is a lot of very nice game development libraries for Java
- If you make games with it, they are cross-platform by default
- Java development environments are some of the best in existence

And for Clojure:
- It allows you to build programs from scratch as you are running them
- It has amazing ability to let you experiment with anything and everything at any time
- A lot of tutorials and books show you how to write games, as well as explain game development
- You get concurrency for nearly free
- It shows how to work with a functional language, while letting you use your Java knowledge
- ...and speaking of, it runs everywhere Java does
- ...and you can use Java code in Clojure, as well as vice versa
- ...or even embed one language in the other, so you can use them at the same time if you want
- Clojure is surprisingly amazing for web dev, so it is worth it even if nothing else comes of it
>>
>>58175624
Write a <programming-language-of-your-choice>-interpreter/compiler
>>
If I am maybe just intermediate level with imperative languages like C++, Java and Python, is it still worth me trying to pick up a functional language or am I better off just sharpening my c++ skills or something?
>>
>>58175834
nobody is better off with c++
>>
>>58175834
Why cant you do both? Its also possible that learning new paradigm might improve your c++ skills right?
>>
>>58175802
> web developer and aspiring game developer, you can seriously benefit from Java and Clojure
> Java and Clojure
> game development
Wew, lad. Also you forgot to mention that most modern imperative languages like C#, Swift and Rust implement most of useful concepts from functional programming, making them the best from both worlds.
>>
>>58175834
I'd say it's definitely worth it, because it gives you more ways to solve problems which can definitely be applied to C++.
>>
>>58174033
"How to Design Programs" should be high up the Scheme list.
>>
>>58175845
Except people who want to be employable (although other languages would get you more money I assume)

>>58175846
I guess so

>>58175855
Yeah I'll continue with it then

The main problem I have is finding things to make, so I don't end up coding a lot and stop improving, so I started learning Haskell a little because of it. Hopefully I can learn some more and think of some cool things to make
>>
>>58175864
I'll be adding this.
>>
>>58175850
Yes, I suggest Clojure for game development.

Even the book the OP suggests for Clojure is about making games, plus there are some very nice libraries for doing just that. You don't even need to target the JVM for this. I mean, a lot of ClojureScript demos are games too, and it does that quite well.

To make things even better, it all has a ton of other benefits for the person asking.

Those were my reasons.

>Also you forgot to mention that most modern imperative languages like C#, Swift and Rust implement most of useful concepts from functional programming, making them the best from both worlds.
Well Java implements a lot of that too. Not on the level of Rust, mind you, but it is enough to be quite nice for some more functional code in the language.

However, none of these languages implement all of the most useful functional concepts IMO.

Then again, I am biased.

>>58175879
>Hopefully I can learn some more and think of some cool things to make
I'm sure computers have annoyed you before, right?
A lot of these annoyances can make some pretty nice little practice projects.

If you have a hard time thinking of a project, try solving one of these..!
>>
>>58175979
>I'm sure computers have annoyed you before, right?
I've recently started doing this (sorting folders, etc) but I haven't ran into anything major. Maybe i just subconsciously ignored them because I thought they would be too difficult
>>
>>58175925
Nice one.
>>
Any haskell resources to get me going in a weekend? I know C, Python, Java, Go and a bit of Common Lisp.
>>
Where can I download Haskell Programming from First Principles?

The link on libgen is an out of date version.
>>
>>58176297
version on libgen is from 2016, surely it cant be that big of a difference?
>>
Why is this allowed?
>>
File: 1479209385681.gif (2MB, 300x262px) Image search: [Google]
1479209385681.gif
2MB, 300x262px
>looking through Haskell book
>mfw this language sucks me in
Well, I should relax and let Haskell suck me whole?
>>
>>58177658
yes
>>
>>58174033
Is Haskell a meme?

Just looks like a bunch of jarble, and i have been programming for 3 years.

Never touched a purely functional language before.
>>
>>58178248
It's a really nice programming language
>>
>>58178248
Meme doesn't mean what you think it means. I get it, you're new here, but please at least try to learn what the words used here actually mean.
>>
Will learning lambda calculus help me learn functional programming or should I not bother?
>>
why did you retards make a separate thread for your meme languages
>>
>>58178588
refer to >>58178618
>>
>>58178614
Yes, it will help immensely
The principles of FP are largely those of lambda calculus, + type systems
>>
>>58178614
>>58178918
But generally learning an FP language means learning lambda calculus.
Haskell for instance.
>>
>>58178918
>>58178938
Cool. Is there a recommended lambda calculus book or something?
>>
>>58178989
I would honestly just recommend learning an FP language.

Here is the lambda calculus:

e,a,b : x
: \x.e
: a b


i.e. an expression such as e, a or b is either a variable (such as x), a lambda abstraction (bind a variable x in expression e) or an application of two expressions

that's it
>>
>>58179050
yay, i learned functional programming
>>
>>58179073
now you need to learn how to use it, and type systems

look at the haskell wikibook in OP
>>
>>58179050
will learning a fp language also improve my lambda calculus? seems you would already have to be somewhat skilled in it to pick up the language.
>>
>>58179175
it's more like lc is the basis of fp

if you can program you can probably figure out LC anyway
learning haskell will involve learning LC

pure LC, by itself, isn't very useful as a language (but it is turing complete, so it can do anything)
>>
>>58179229
How does one make Brainfuck compiler in LC?
>>
I need women "programming" images please, thank you.
>>
>>58179624
A lot of effort.
Pick an FP language and learn it
>>
I might be dumb.
https://en.wikibooks.org/wiki/Haskell/Lists_and_tuples
Why
cons8 [True,False]
doesn't work?
>>
I'm on chapter 3 of learn you a haskell for great good, should I switch to the link in the OP?
>>
>>58181115
What do you mean?

const [True, False] is a function returning [True, False]

>>58181235
LYAH is fine
>>
>>58181115
>>58181247
It's cons8, not const. The types are not the same, lists contain elements of the same type.
>>
>>58181264
But tuples do?
What's the difference in their machine realisation?
>>
>>58181304
Yes, and tuples with different element types are different tuple types, eg

(True, False) :: (Bool, Bool)
((), False) :: ( (), Bool)
(3, "hello") :: Num a => (a, String)
>>
>>58181366
i never understood that "Num a =>" thing
>>
>>58181379
It's a constraint
You can think of it as meaning "where a is a numeric type"
>>
>>58181379
>>58181402
So for instance
The type of (+) is
Num a =>
a -> a -> a

i.e., a -> a -> a, where a has a Num instance (you should learn about type classes later)
>>
>>58181402
ah that makes sense
>>
>>58181421
Can you set multiple constraints?
>>
Can someone explain to me why some people on /g/ consider functional programming better than OOP?
>>
>>58181443
Yes.
It's the same syntax as a tuple type.

(Num a, Eq a, Ord b) =>
>>
>>58181460
OOP isn't very good, and hasn't got any real basis other than trying to vaguely turn language into programming language ("nouns and verbs")

FP has strong foundations
>>
>>58181460
It's not better, it's just better than false OOP languages like Java, where "everything is an object' isn't entirely true. The only reason Java doesn't support multiple inheritance, is because they were too lazy to solve the diamond case, so they came up with the hack called interfaces. It's not just my opinion here, even Uncle Bob thinks the same. SmallTalk did OOP right. Python does OOP right.
>>
>>58181485
I am complete noob and i just started learning c# (so OOP), i have to admit, f# sure looks sexy, but i still can't find any characteristics of functional languages that makes it more preferable than OOP languages
>>
>>58181509
Do you use LINQ?
Do you use lambdas?
Do you use that async syntax?
Do you use tuples?
>>
>>58181460
Functional programming is incomprehensible to code monkeys, with their tiny brains. It makes us feel superior.
>>
>>58174033
Where is JAVASCRIPT????
fucking weeaboo
>>
>>58181532
Not programming
Not a language
Not functional
Not functional
>>
>>58181509
Look up the Facebook Spam Filter. It's written in Haskell. Look up the Ocaml teams at Facebook. Functional programming works better in a lot of cases(not all), so it's used where preferable.
>>
>>58181524
>Do you use LINQ?
Yes, very useful

>Do you use lambdas?
same as above

>Do you use that async syntax?
written first yesterday and fucked up, UI thread is still freezing, i have to learn way more about async/await

>Do you use tuples?

no
>>
>>58181548
those are all functional
>>
>>58181564
I use those in c#, i don't get it
>>
>>58181597
C# is not a strict OOP language, it has a lot of very useful functional elements brought over from F#
>>
>>58181597
those are functional features of C#
>>
>>58181606
So, linq was in f# before c#? interesting, i didn't know that
>>
>>58181620
I don't think that F# had LINQ originally, but that's aside the point.
>>
File: lWgJ4NI.gif (206KB, 500x498px) Image search: [Google]
lWgJ4NI.gif
206KB, 500x498px
>>58181536
only retards don't understand js
got news for you , it is a language
of course it's functional, JS can do everything, that's why ur butthurt
>>
>>58181620
does LINQ have a function that takes a single element and converts it into an enumerable?

>>58181654
JS isn't functional in either sense of the word
a language is, by definition
"the method of human communication, either spoken or written, consisting of the use of words in a structured and conventional way."
>structured
>>
Javascript has no place in this thread. Leave.
>>
Tuple can contain any types but it's fixed.
List is free but it can contain only one type.

Why?
Why it's so useful?
Can I contain any type and not be fixed?
>inb4: list of tuples
>>
>>58182354
The only function that would work on such a type would be the length function or (:).

You're better off just using natural numbers.

To explain that, you need to start realising that what you can do to a value depends on what its type is.
You can add integers.
What if you had to make something that works with ANY type?
Well, you can't add literally any type. So that's out of the question.
You can't display it as a string.
You can't do anything, because you don't know anything about it.
All you can work with is the structure.

It's like having a list of unit values.
>>
>>58182457
>All you can work with is the structure.
I mean if you've got a list of any type.


You CAN have useful heterogeneous lists, using existential types. But it's rarely what you want.

The thing about existential quantification is it's like this:

data Showable = forall a. Show a => Showable a
instance Show Showable where
show (Showable x) = show x


Since we can only put "things that can be shown" into the box (the Showable constructor), then we know, by discovering the showable constructor, how to show that item.
It's like OOP style polymorphism.
>>
>>58182500
So in this example,

Showable 3
and
Showable False
both have the same type. So you can put them in a list

[Showable 3, Showable False, Showable "Hello"]
>>
Haskell can compile to C, right? Is there any way for me to see the C code equivalent of a Haskell program?
>>
>>58183266
Maybe you should try looking at the C-- code instead

{-# OPTIONS_GHC -ddump-cmm #-}

pragma, or just the flag -ddump-cmm
>>
>>58183266
>>58183363
You can also look at the core language
GHC has like 3 intermediate languages or something
>>
In what irl situations and by irl I mean work, have you used functional programming?
>>
>>58183570
http://paulgraham.com/avg.html

Now STFU
>>
>>58183570
exactly the same situations i've used OOP and imperative programmign
>>
>>58183702
So why should I learn functional if it is the same? Does it just feel better to write and read or what?
>>
>>58183809
It is a new way of thinking. You shouldn't always think so narrowly. Sometimes, learning a new language makes you a better programmer, just because it broadens your thinking space.
>>
File: datomic-logo-290x230_1.png (3KB, 147x117px) Image search: [Google]
datomic-logo-290x230_1.png
3KB, 147x117px
>>58183570
Just a few of the companies using clojure along with datomic

Amazon
Netflix
Apple
Deutsche Bank
Salesforce
Soundcloud
Staples

and many more.

Functional programming is still used loads. But they are behind the curtains doing the job big companies need. And not making a fuss or trying to look fancy for nu-coders in starbucks yacking about small startups.
>>
Just a quick question, I need a function that takes the difference of two functions.
f :: a -> b
g :: a -> b
[\code]
I could always write a lambda expression
 diff f g = \x -> f x - g x 
but was wondering if there was a more haskell idiomatic way to define diff?
>>
>>58183841
Strangely I never thought about it that way. So where do you recommend to start? What language, what book?
>>
>>58183943
Nobody uses Lisp these days, but it is the most powerful language on Earth, and it was created 50+ years ago. Get a weekend in Lisp, write some recursive functions, get to macros, and then you can safely get to more modern functional languages like Haskell, Clojure or OCaml(used extensively at Facebook), or Erlang(if you want to write highly concurrent architectures - Golang is for normies, doesn't even come close).
>>
>>58184122
>Nobody uses Lisp
>Clojure
Yeah what?
>>
>>58184122
>>58184166
I meant Common Lisp
>>
>>58183923
I'm not sure but that looks sort of common
There's definitely a function for doing something like

f x - f y
f x * f y

But I'm not sure about your case. Maybe in Arrows?
>>
>>58183923
>>58184242
Oh shit, I just thought.
If you've got profunctors,

dimap f (lmap g)

this is also the same as

lmap f . rmap (lmap g)


(->) is a Profunctor
rmap (lmap g) is because
a -> (b -> c)
is a nested profunctor, so you're saying "with the right hand side (b -> c), lmap it"
>>
>>58174033
Why the hell do some general threads insist on having so much shit in the OP?
Why don't you take from /dpt/'s example and just have one line of shit and the old thread link?

This general doesn't even need to exist, anyway.
>>
>>58184322
>how dare you be different
>i demand you cease existing!
>>
>>58184351
I don't think functional programming is bad.
It's just I don't think you're so snowflake special that there needs to be a slow-ass general for it, instead of just being a part of /dpt/.
/g/ has was too many fucking generals as it is.
>>
>>58184372
You do realise /prog/ used to be its own board, right?
>>
>>58184288
>nested profunctors
Do you think I should learn category theory in parallel with haskell? All this terminology is killing me.
>>
>>58184399
Yes. And?
>>
>>58184399
Why was it deleted? I want to say not enough interest, but we still have /gd/ so...
>>
>>58184438
I think moot just didn't want to have the old textboards anymore.
>>
>>58184453
What are "old textboards"? Text only?
>>
>>58184478
>Text only?
Yes, obviously.
I said "old" because we sort of have "new" text-boards with boards like /news/.
>>
>>58184407
It's basically specifically about Haskell typeclasses/implementations rather than category theory itself, but it's like this:

A functor:
fmap :: (a -> b) -> (f a -> f b)


A bi-functor:
bimap :: (a -> x) -> (b -> y) -> (f a b -> f x y)

This is like a functor on two type variables.
A pair constructor (,) or a type like Either are examples of this.


A pro-functor
dimap :: (x -> a) -> (b -> y) -> (f a b -> f x y)

A lot like a bi-functor, except for one difference.
Notice how this has its first argument function flipped around.
It's contravariant.

Now imagine you have (->), the function type constructor.

You've got a type, say (a -> b), and you want to transform it into (x -> y)
You want to apply a function to the input, and a function to the output.
Transforming the output is simple and obvious - in fact, (->) a is a functor.
fmap = (.)
We just need some function (b -> y) and we can compose our original with that.

But to transform our input, we need a function that _outputs_ our old (a) type.
A function (x -> a). This is "backwards" compared to a bi-functor, or a functor.

A pro-functor is "backwards" in its first type parameter and forwards in its second.


By "nested" I just mean that
(a -> (x -> y)) is a profunctor
(x -> y) is also a profunctor

So we use rmap to transform (x -> y)
and then lmap to transform the x inside of the (x -> y)
>>
>>58184242
I'm an idiot

Using that same function

(-) `on` ($x)


(on is in Data.Function)
>>
>>58184122
>Erlang
Don't forget Elixir.
>>
>>58184821
Should have caught that, thanks mate
>>
>>58184649
Hold up, given a function
 a -> x 
, how do we have a way to map
 x -> a 
? Wouldn't an inverse function be nigh impossible to create (Computationally, I mean)?
>>
>>58185186
You can't turn an (a -> x) into an (x -> a), that's the point.
A profunctor is contravariant in its first/left type variable.
So lmap (which maps its left argument) requires a function that goes "the other way", i.e.
rather than

fmap :: (a -> b) -> f a -> f b
we have
contramap :: (b -> a) -> (f a -> f b)
>>
I have experience in Java and ruby. I feel guilty for not knowing c, but it doesn't interest me. Should I learn c before any fpt languages if I already have programming/Comp sci experience? Even if I don't learn c now I am already enrolled to study computer engineering this fall and I will probably learn it then, so it's not like I am skipping out on c all together, just not letting it take up my free time.
>>
>>58186474
C won't help you learn FP
>>
>>58186474
Also, which /fpt/ language do you think would be best to learn first?
>>
>>58186496
If you're coming from Java then maybe you should try C# and then F# before moving on to proper FP
>>
>>58186496
>>58186546
if not and you feel like challenging yourself, just go straight to haskell or ocaml or even scheme
>>
>>58186496
Scala uses similar syntax to Java. It also has a decent IDE in eclipse. You can write imperatively at first and slowly slide into FP. The only downside is that it's so similar to java, you'll rely on your java knowledge too much and wont actually learn any functional techniques.
>>
Does something like this add a :[] at the end because of the return type?
repeat :: a -> [a]
repeat x = x:repeat x
>>
>>58186675
(:) is the cons operator, it takes an a and an [a] and gives back an [a]
you can think of (x:xs) as "x followed by xs"

repeat x = x : repeat x

"repeat x" is x, followed by "repeat x"
if you substitute that in:

repeat x = x : x : repeat x
repeat x = x : x : x : repeat x
repeat x = x : x : x : x : x : x : x : repeat x

etc
>>
>>58174033
Will learning functional languages help me w/ becoming an ethical hacker? Mostly been studying scripting languages and backend webdev ones.
>>
>>58186824
>ethical hacker
No, but have you considered buying a fedora?
>>
>>58186741
Fuck, I'm retarded. Thanks.
>>
>>58186867
Yes, I already own 2. I shit you not.
>>
File: Kuro.png (45KB, 250x317px) Image search: [Google]
Kuro.png
45KB, 250x317px
>>58186824
>>58186996
Oh good. Then you are all set.

A very important thing you will need to do is not technical, but to learn the mindset of trying to figure out problems in a domain you know nothing about. For example, here are a set of talks from someone who now works for Project Zero, about what she learned when trying to hack Tamagotchis:
https://www.youtube.com/watch?v=WOJfUcCOhJ0
https://www.youtube.com/watch?v=b_PZX6t_EF0&t=1219s

The next step is to get acquainted with the internals of the systems you want to hack. If you are focusing on local programs, that means getting familiar with how a compiler works, as well as the assembly it emits. This is a good tool for playing around with the results of C compilation and it's assembly: https://godbolt.org/

After you know at least how to invoke functions, try to make programs crash, and explore them in the debugger of your choice, to see if there is anything you can do with the problem you found. This is more of a mindset thing as well, except a far more technical take on it. A good book on this is A Bug Hunter's diary: https://www.nostarch.com/bughunter

If you are focusing on web stuffs, assembly stuff is still good to know if you manage to find a problem with a server--but you would also need to get acquainted with things like SQLi, XSS, CSRF, and popup security problems (Like window.opener access, Google that). A good site for learning this is Hack This Site: https://www.hackthissite.org/

After you are familiar with how to break the stuff you want to break manually, then you can start writing your own special tools for attacking specific programs, services, connections and so on.

Of course, after all this, you still need to read a lot. The netsec and reverseengineering subreddits are both ridiculously good sources of security blogs and for capture the flag challenges. There are also hiring threads there to get on board as a security researcher in some pretty neat places...or you can take on bug bounties.
>>
File: Lisp Cube.webm (2MB, 1920x1052px) Image search: [Google]
Lisp Cube.webm
2MB, 1920x1052px
>>58187348
Now, how can a functional language help you with this? Lisps in particular are fantastic for exploring things as you are writing code for them.

If you have a REPL, you can both experiment AND write the code whenever you find something out, and incrementally and piece by piece explore and work with very often hard-to-find problems in systems.

Pic related is not mine, but shows what I mean. The thing on the right is a running program, and you can hotswap and change anything while it is running. This sort of thing is incredibly useful when you are trying to experiment and trying to write code for things you are not overly familiar with.

As for languages, I think Clojure is probably the best for this because because of the large number of Java and Clojure libraries available that can help you test and research things.

Good luck..!
>>
>>58187406
Oh, I forgot to mention why functional itself was good for this sort of thing.

That mostly boils down to functional allowing you to avoid needing to have to set up state in a way for your testing.

The paradigm would mean that instead of having to set an alphabet soup of things between each and every test, you just change the function call you test with, or change some function along the way and try again.

It is very nice for learning about something quickly, and is the benefit over non-functional languages with REPLs like say, Ruby. (Because Pry is actually kind of amazing)

However, you can write functional code in non-functional languages too, so in the end, it is all more the style of code you write more than it is the language you use. Especially because the optimizations functional languages make for the paradigm are not needed as much when you aren't writing something intensive.
>>
>>58187497
Not >>58187348, but that was a great answer. I had never considered functional languages for those purposes.
>>
>>58175834
Java and Python have already adopted some functional elements. Even JavaScript allows a fairly functional style these days.

Functional programming has its advantages and you will see more of it in the future. It's always good to learn something new. I would say the most important thing about functional programs is lambdas, or anonymous functions. You could start with Scala if you want. It allows imperative style, but tries to get you used to functional. It's a much easier transition than diving straight into Haskell.

Just install scala from here:
http://www.scala-lang.org/download/

Install IntelliJ IDEA with the Scala plugin and start reading "Programming in Scala (Odersky, Spoon and Venners)", to be found on bookzz dot org.

You NEED to read the book for it. Start with chapter 2.
>>
>>58186562
Thanks for the reply, I think I am going to go with scheme, as that is what is taught in SICP, so I can work through the book and not worry about learning another language on top of it.
>>
>>58187673
>It allows imperative style, but tries to get you used to functional
I thought opposite of imperative is declarative, but not functional.
>>
So, the two main languages I see in this thread are Haskell and Clojure.

What are their main differences? Also why would I want to learn one over the other?
>>
my left bracket key is broken on my keyboard, what languages can i write in without getting fucked
>>
>>58189554
Brainfuck :^)
>>
>>58189554
Put this in your .emacs:
(global-set-key (kbd "C-;") (lambda () (interactive) (insert-char ?[)))
(global-set-key (kbd "C-:") (lambda () (interactive) (insert-char ?})))


Now you can type "[" with Control ; and "{" with Control :.

Be free! Use whatever language you want.
>>
Im going through Haskell programming from first principles and this is really good book. Probably one of best programming books i've ever read.
>>
>>58189691
[spoiler]i use vim[/spoiler]
>>
>>58189727
Is it available anywhere for less than 60$ (preferably 0$)? I can't drop that much on a book, but I'm quite interested in checking it out.
>>
>>58189846
>2017 - 0.013
>don't know how to pirate books
http://bookzz.org/book/2597529/702391
>>
>>58189864
Sorry, couldn't find it.
Thanks!
>>
>>58189846
>http://gen.lib.rus.ec/
Only and best site for books in entire universe
>>
File: 1450715823996.jpg (66KB, 774x672px) Image search: [Google]
1450715823996.jpg
66KB, 774x672px
>>58175624
no... there are no practical uses.
>>
>>58189774
Then:
:imap <C-o> [
:imap <C-p> {


Control + o and Control + p should do it then.

You can't map two things with difference cases to two different things in Vim, and I don't think you can map C-: or C-; in this case either.

I had assumed Emacs because of the thread.
>>
>>58189953
Not him, but should I learn emacs for haskell? Currently in the process of learning vim.
>>
In haskell given Just [1..10] how can i apply lets say (+1) on it?
>>
>>58189985
Im writing haskell using vim, i dont see why not its comfy
>>
>>58189985
What do you mean?
Just use the text editor you like, doesn't matter what language you use.

Also: daily reminder that Vim > Emacs.
>>
>>58189985
>>58190026
> Implying you have to choose one
> They don't know about spacemacs
>>
>>58189985
Well, I write Clojure and don't know Haskell, so I couldn't tell you like >>58190015

Emacs does have a Haskell mode called Hugs which allows you to have a running Haskell process in your editor that you can evaluate code to, so that might be useful to you in the same way CIDER is useful for me.
>>
>>58189987
foldl (+) 0 [1..10] iirc.
>>
>>58189987
λ fmap (map (+1)) (Just [1..10])
Just [2,3,4,5,6,7,8,9,10,11])
>>
>>58190065
Why does that work?
fmap :: Functor f => (a -> b) -> f a -> f b

So fmap takes function from a to b and applies it within f a to get f b?
>>
File: flycheck.gif (188KB, 1100x744px) Image search: [Google]
flycheck.gif
188KB, 1100x744px
>>58190052
Intero is more like CIDER than Hugs: https://commercialhaskell.github.io/intero/

It requires Stack though.
>>
>>58190095
> Why does that work?
Because Functor class is implemented for Mayby type: https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base-4.9.0.0/src/GHC-Base.html#line-651 .
> So fmap takes function from a to b and applies it within f a to get f b?
Yes, it's a generalization of map.
>>
>>58190127
Why cant i do fmap(+1) [1..10] as with map?
>>
>>58190147
You can, fmap for lists is just an alias for map:
λ fmap (+1) [1..10]
[2,3,4,5,6,7,8,9,10,11]
>>
>>58190192
Sorry i meant fmap (+1) Just [1..10]
>>
>>58190256
Because this expands to
(+1) [1..10]
and you can't apply a scalar function to a list, you need to use map for that. So you first need fmap to get inside the Just and another (f)map to get inside the list.
>>
File: 45236499_p3_master1200.jpg (97KB, 400x400px) Image search: [Google]
45236499_p3_master1200.jpg
97KB, 400x400px
>>58174033
>Common Lisp in topic
Yay, I'm glad I found this thread.

>>58187406
I made that webm. Pretty happy someone saved it. Motivates me to write more Common Lisp to show off cool things and talk about it again.
>>
File: 1414755619067.jpg (49KB, 600x900px) Image search: [Google]
1414755619067.jpg
49KB, 600x900px
>>58190289
I saved it because it shows off really well what a REPL actually means for Lisp. It was also a pretty cool example.

I miss the Lisp generals we used to have.
>>
>>58190283
Ooooh right right, thanks alot!
>>
>>58189516
Haskell is a strongly typed ML derivative
Clojure is a mostly dynamically typed lisp
>>
>>58190333
If you learn something called monad transformers (probably a while away for you) then there's a way you can do this
>>
>>58190312
How is Common Lisp mascot called?
>>
File: fp.png (25KB, 631x98px) Image search: [Google]
fp.png
25KB, 631x98px
fp fags will actually defend this
>>
>>58174033
haskell from first principles is a behemoth of a book, but looks like a pamphlet in the image. why
>>
>>58191538
a list of primes should be called primes

>>58191571
very small font
>>
I'm going to challenge myself to write an imageboard for the first time as fast as possible in Common Lisp. If anyone wants to use whatever tools and race along just for fun, I don't think I'm a very good programmer so I won't be hard to beat. I'll start in about 6-7 hours after I sleep and eat.

>>58190312
I kinda feel the same.

>>58191420
I don't think there's a name, it's just an alien.
>>
File: 1268891287892.jpg (28KB, 337x404px) Image search: [Google]
1268891287892.jpg
28KB, 337x404px
>>58191583
> as fast as possible
> in Common Lisp
>>
File: CVTATdZUEAAmg5M.jpg (41KB, 400x700px) Image search: [Google]
CVTATdZUEAAmg5M.jpg
41KB, 400x700px
>>58191420
The mascot is just the Lisp alien from Land of Lisp, and the girl is the Lisp Alien kid. Neither are really official language mascots, though they are both reasonably common when talking about Common Lisp.

There aren't too many drawings, but the Lisp alien was made by Conrad Barski and the girl was made by marutakoEX.

>>58191608
Say what you will, but if you are good at it, you can build things from scratch in Common Lisp in amazingly short times.

You can owe that largely to a combination of the REPL and language features.

Common Lisp isn't really functional by default though.

>>58191583
Good luck!
>>
File: 1470051795287.png (37KB, 256x223px) Image search: [Google]
1470051795287.png
37KB, 256x223px
>>58191742
There really isn't enough art.
>>
Is it true that you can implement Lisp in Lisp?
>>
>>58192311
Yes:
 (loop (print (eval (read)))) 
>>
>>58191742
dat's kawaii

reminds me of Asui Tsuyu...
>>
>>58192311
You can implement any language in any other language, as long as they're Turing complete, which is true for pretty much every programming language.

inb4 finite tape - no one cares
>>
>>58192341
Aka: The source of the word "REPL"
>>
>>58175624
Haskell is an amazing prototyping language with almost no useful application in the modern age. I'm sorry anon.
>>
File: 1436479456450.jpg (29KB, 220x241px) Image search: [Google]
1436479456450.jpg
29KB, 220x241px
>>58192414
Didn't you hear that Python 3 isn't Turing complete because it can't run Python 2 programs?
>>
>>58192587
A good old Zed Shaw.
He also thinks FSMs are a software design technique.
>>
>>58192587
As a Rubyist, I am very sorry that you inherited him from us.
>>
Haskell fags define me a functions with types and show example of usage
foo::a->()
bar::()->a
>>
>>58193271
foo :: a -> ()
foo _ = ()
-- foo = const ()

-- usage of foo
-- a function in Control.Monad
void :: Monad m => m a -> m ()
void = fmap foo


bar :: () -> a
bar () = bar ()
-- not very useful
>>
>>58193271
foo = const ()
bar () = undefiened
>>
Anyone got some exercises (Beginner-intermediate level) for Haskell? I need to study and do these exercises by hand.
>>
>>58194960
Does the wikibook or Haskell from First Principles not have exercises?
>>
File: 1482434797413.jpg (17KB, 467x332px) Image search: [Google]
1482434797413.jpg
17KB, 467x332px
>Indentation is Haskell matters, but yet it's optional
I'm glad to see common sense involved in language design.
>>
>HaskLEL is useless and, pure functional and stateless languages are a meme
>Lisp is only used for third class text editors
>SICP is not a good book and this is why it's given away for free
>Anime is shit
>Maki is slut
>C++ is much better than C
>Java is the language of the future
>JS is a good choice for server side
>The best software engineers are indians
>>
File: a series of false statements.png (13KB, 540x195px) Image search: [Google]
a series of false statements.png
13KB, 540x195px
>>58195288
>>
>>58195288
[openparenthesis] thou [closeparenthesis)
>>
>TFW get sleepy while coding.
Why is this demanding so much from me?
How do I get used?
I can nap for now, but what do while in work?
>>
File: 1482236145668.png (307KB, 500x500px) Image search: [Google]
1482236145668.png
307KB, 500x500px
What salary can I expect in my Haskell job?
How much do you earn?
>>
>>58195472
sleep properly
>>
>>58195488
300k starting
>>
>>58174033
add http://www.purescript.org/learn/
and http://ocaml.org/learn/tutorials/ (not great but it's how I learned :^) )
Also, I think you should probably add topic-specific books, like Purely Functional Data Structures. Maybe we should make something like the gentoomen library?
>>58195472
stim up mate :^)
>>
>>58195499
Only if you have a maths phd.
>>
>>58195499
Fucking beat me to it.
>>
>>58195499
No seriously, how much do people in here make? :^)
>>
>>58195855
I gross about 10000 (You)s per annum
>>
>>58195878
kek
>>
>>58195855
Anyone can learn and write any language. Being a good mathematician and engineer is what differentiates you.
>>
>>58196175
What about people with dyslexia?
>>
>>58196579
they can still learn it. doesn't mean they'd be any good at it
>>
>>58196642
>learn ___AND WRITE___
>>
>>58196680
>>58196579
They can still lean and write.
>>
>>58190034
spacemacs is clunky shit

just get evil mode and optionally spacemacs-theme and spaceline
>>
>>58196999
>I want to spend hours to install and tune dozend of emacs packages
>Instead of just enabling spacemacs layer by adding a single word to the config
>>
>>58197047
what, you need to add like 4 lines to your init to get those to work
>>
>>58196999
>>58197047
>>58197059
or just use Vim like a sane person.
>>
>>58197059
Spacemacs isn't just emacs+evil, the main thing is layers - http://spacemacs.org/layers/LAYERS.html . Like, all you have to do to add support for Java for example is to add "java" to dotspacemacs-configuration-layers in your config, and it will automatically install and configure dozen of packages and provide sane and uniform interface to them.
>>
The haskell book OP posted is painfully slow. It thinks I'm a retard, so it has to explain everything, what a programming language is, etc. I also have OCD when it comes to reading, so I have to read EvERYTHING. Any other book, that already skips the trivial stuff?
>>
>>58197699
Sounds like you're hardcore enough for 'Real World Haskell'.
>>
>>58197699
Just skip chapters, or skim the articles?
>>
>>58175788
>- It's faster
ATS is the fastest language behind C and C++.
>>
How many Haskell libraries are there?
How many functions do they include?
>>
>>58197873
about tree fiddy
>>
>>58197873
a lot
a lot
>>
>>58197838
ATS is theoretically as fast, if not faster than C
it's also an imperative language though
>>
Is Learn You A Haskell book any good?
>>
>>58198037
if you don't know programming it's ok, I like real world haskell better though, more straight to the point.
>>
>>58198020
>ATS is derived mostly from the ML and OCaml programming languages. An earlier language, Dependent ML, by the same author has been incorporated by the language.
>>
>>58150501
It's not a wishlist, you don't need to have all of those things.
>>
>>58195472
You could have sleep apnea. I was tired all the time before I got diagnosed and started treatment for it
>>
let tau = 2 * pi
>>
>>58198104
What kind of treatment did you get?
How is your sleep now?
You don't feel tired now, do you?
>>
>>58198140
I sleep with a CPAP machine every night now. I usually sleep through the whole night and I don't feel tired all day anymore.
>>
>>58198058
so was Rust
>>
>>58197911
And how am I supposed to learn them all and feel the power of Haskell?
>>
>>58198260
Rust is pretty functional, though.

Way I see it, there's a spectrum between imperative and functional based on how many "functional" features a language implements. Functional programming is more or less a refinement of imperative programming.
>>
Can anyone recommend a news/info website focused on programming or software engineering? Other aspects of computer engineering are ok too, but I mostly want to keep up with new developments in programming languages without following every one of them.
>>
>>58198281
>learn ALL the libraries
???
>>
>>58198300
Hacker News or Lambda the Ultimate if that shit is still even alive
>>
>>58198308
Thanks a lot, subscribed to both.
>>
>>58198307
They come in standard distribution, how come they are not a part of language that you may not know?
>>
>>58198362
Oh you mean the standard library?
It's not too big.
Probably the same size or smaller than C++'s.
Check hackage.

Though the nice thing about Haskell is that you don't really need to use the standard library.
You could build almost everything from scratch.
>>
A billion generals. And this board is still trash.
>>
>>58198393
>You could build almost everything from scratch.
But is there any language that doesn't let that? (excluding esoteric ones)
>>
>>58198573
I think that anon means building it all from scratch wouldn't be difficult. most of the code in the standard library could probably be written by a beginner and in very little time
>>
>>58189987
map (+1) <$> Just [1..10]
>>
Anyone got this pdf?

Haskell Programming: From First Principles
>>
File: book_cover-min.png (73KB, 2480x3507px) Image search: [Google]
book_cover-min.png
73KB, 2480x3507px
>>58199864
>>
>>58199864
don't, it's only one guy (probably the author) shilling it here
>>
>>58199878
https://0x0.st/pbp.pdf
>>
>>58200474
thx a lot bro
>>
>>58200474
One thousand seventy six pages
>One thousand seventy six pages
One thousand seventy six pages
>One thousand seventy six pages

Something inside me says that wikibook will be more efficient.
>>
>>58200544
thicc
>>
Nice thread, I would like to say thanks to all you guys. I am leveling myself with this book. Pace is nice and it's very informative.

http://www.cs.cmu.edu/~dst/LispBook/book.pdf
>>
What's an interesting project that I could do with a functional programming language
>>
>>58200667
>tfw actually bought the book
>read it with half attention
>found out it was available online
fuck me, wasted money
>>
>>58195566
>>58200474
>>58200667
I'll be adding those.

Elixir, Elm, PureScript are Web languages?
I guess they must be united under one category.
Anyone?
>>
>>58200782
Anything.

FP languages are especially suited to language/compiler stuff, though.
>>
>>58200782
Write a Wolf3D clone.
>>
is there any resource on basics of functional programming languages in general, like monads, not having side effects, etc, as opposed to specific languages? thank in advance
>>
>tfw actually find myself really needing HKT in Rust
Welp, I was putting off making my language but I'm gonna do it now.
>>
>>58201083
just read papers from academics
>>
>>58201083
Wadler's monad paper talks about that stuff. It uses Haskelly pseudocode but IIRC other than that it's pretty general.
>>
>HaskLEL is useless and, pure functional and stateless languages are a meme
>Lisp is only used for third class text editors
>SICP is not a good book and this is why it's given away for free
>Anime is shit
>Maki is a slut
>C++ is much better than C
>Java is the language of the future
>JS is a good choice for server side
>The best software engineers are indians
>>
>>58200973
Elixir is not a web language.
>>
>>58200973
elixir is not necessarily web oriented. it runs on BEAM (the Erlang VM) so they're pretty much identical in their use.
>>
>>58201569
>Lisp is only used for third class text editors
Second-class too.

Someone made an embedded Clojure editor that fires up a server you can connect to in a browser to edit the code of your project. Except, when you save, it actually applies to the runtime immediately. It also gives you browser access to your REPL.
https://sekao.net/nightlight/

>JS is a good choice for server side
Cool thing about this actually--because of Macchiato, Figwheel actually works with NodeJS now.

If you don't know why that is amazing, it means that you can change your ClojureScript server code and never have to restart your node process. It just gets swapped in and you can build incrementally on Node like you would Clojure.
http://yogthos.net/posts/2016-11-30-Macchiato.html
>>
>>58202684
I think both of these things are really really cool, damn.
>>
>>58198573
Yes, plenty.
>>
>>58202864
Clojure has the coolest projects.
>>
>>58204601
Such s?
>>
>>58205324
C#, Java
>>
>>58200936
Do you have a 2013 edition?
>>
>>58200973
You can add, "Slime - The Superior Lisp Interaction Mode for Emacs" it's epic.

Also easymod "emacs.d" configs for webdev and bunch of the functional languages https://github.com/purcell/emacs.d.
>>
>>58205609
I didn't know there was a book for SLIME.

These days I use Sly because it makes it easy to manage multiple REPLs into the same Lisp, but SLIME is still kind of amazing in a lot of ways.
>>
>>58205694
lol I am not sure that there are book for a slime. That was just a repl suggestion, my bad
>>
>>58205721
Ah. A book on that would be nice though, Lisp tooling isn't super user-friendly initially, but they have some really cool tricks that you simply can't compare to other languages.

The problem is that whenever you talk about them, you sound like some sort of out-of-touch zealot because the barrier to entry for knowing about the really cool stuff is higher than basic curiosity might bring someone.
>>
File: 1437658095455.png (104KB, 358x242px) Image search: [Google]
1437658095455.png
104KB, 358x242px
>>58205423
You could have just said Scratch.

2/10, wasted opportunity.
>>
>>58205760
>... you sound like some sort of out-of-touch zealot because the barrier to entry for knowing about the really cool stuff is higher than basic curiosity might bring someone.

True, but in my case that's currently the only way to think and to implement my 'thoughts'. Somehow it keeps me from furious stack-overflow copy/pasting. Lack of heavy digested, instant code is really helpful.
>>
>Common Lisp
You may as well call Tcl/Tk functional
>>
>>58206133
It's just Tcl, but I get it. I actually think that CL is the least functional language by default because of the nuclear scale that you can manage state with macros.

But unlike other languages like this, you can make it reasonably functional. Land of Lisp even goes all the way to make you implement a lazy tree that you recursively and functionally can traverse.
>>
Can someone point me at differences of functional and logic programming?
I feel like I'm learning the same but under different names.
Let's say, pattern matching.
skin "Tyrone" x = {x = "Black"}

is not much different from
skin(tyrone, X) :- X = black.

What am I doing wrong?
>>
>>58207073
They have a lot of overlap
>>
>>58207125
What do they have different then?
>>
Why is Idris' ifThenElse not dependently typed?

i.e.

%hide ifThenElse

oldIf : {x:Type} -> Bool -> x -> x -> x
oldIf True x _ = x
oldIf False _ y = y

ifThenElse : {x:Type} -> {y:Type} -> (b : Bool) -> Lazy x -> Lazy y -> oldIf b x y
ifThenElse True x y = x
ifThenElse False _ y = y
>>
File: dependent if.png (5KB, 808x81px) Image search: [Google]
dependent if.png
5KB, 808x81px
>>58207750
like this
>>
>>58207750
>>58207793
How are you going to typecheck the code if you don't know the return type at compile-time? It's like overcomplicated dynamic typing shit.
>>
>>58207817
The return type depends on the input value.
This is how dependent typing works.
>>
>>58207817
>>58207862
e.g.
putStrLn is a function from a string to an IO action

s = if b then "Hello" else [1 .. 3]


you cannot* do

putStrLn s


the type of s is not String, its type is (oldIf b String (List Integer))

*if there was a proof or some evidence that b was True, then the compiler might be able to do this
>>
>>58207862
Yeah, I have a vague idea, but I don't get how it works in the real world, like if you have
b <- someRandomUserInput
let a = if b then 1 else [1, 2, 3]

how are you gonna use it?
let b = a + 1 //?
let l = length a //?

You can't do anything useful before you check the actual type at run-time, and so it becomes dynamic typing shit.
>>
>>58207924
No, the types are checked at compile time.
There would be a run-time check if you can't handle both types.
>>
>>58207965
How is that different from "if" returning Either Int [Int]? You still have to do runtime checks to do something useful.
>>
>>58208251
The dependency on a variable is encoded in the type.

If you find out b is a certain value in a certain path, then you find out more about the type
>>
Clojure and Erlang are not functional languages, please stop claiming that they are.
>>
>>58208841
What if they will be claimed?
>>
In Haskell, why do I have to use two maps on a list of lists?
>>
>>58209039
Because you use map to go from
(a -> b) to ([a] -> [b])
So you use map again to do
([a] -> [b]) to ([[a]] -> [[b]])

If you use monad transformers you don't have to, but then you can't map each list.
>>
>>58209039
Why shouldn't you? What would you expect to be the output of
map length [["cat", "dogs"], ["foo", "bar", "fizz", "buzz"]]
>>
What's short Haskell tutorial?
>>
>>58209227
Learn You A Haskell is a relatively quick introduction
>>
>>58209227
fizzBuzz :: (Show a, Integral a) => a -> String
fizzBuzz n =
case (n `mod` 3, n `mod` 5) of
(0, 0) -> "FizzBuzz"
(0, _) -> "Fizz"
(_, 0) -> "Buzz"
_ -> show n

main :: IO ()
main = mapM_ (putStrLn.fizzBuzz) [1..100]
>>
>>58209237
What's other?
>>
>>58209039
map :: (a -> b) -> [a] -> [b]
map . map :: (a -> b) -> [[a]] -> [[b]]
map . map . map :: (a -> b) -> [[[a]]] -> [[[b]]]
...
>>
>>58209346
fmap fmap fmap
>>
>>58209227
>>58209299


fizzBuzz :: (Show a, Integral a) => a -> String
-- the type of fizzBuzz is (a -> String)
-- for any a that is integral and show-able
-- (can be displayed as a string)

fizzBuzz n =
-- n is the first parameter to fizzBuzz
-- this is syntax for fizzBuzz = \n -> ...
case (n `mod` 3, n `mod` 5) of
-- Pattern match on a tuple consisting of n modulo 3 and n modulo 5
(0, 0) -> "FizzBuzz"
-- if n % 3 is 0, and n % 5 is 0, the result is "FizzBuzz"
(0, _) -> "Fizz"
-- _ is a stand-in for "any value"
-- i.e. "if n % 3 is 0" ...
(_, 0) -> "Buzz"
_ -> show n
-- _ can stand in for anything, including tuples
-- _ is the same as (_, _), (_, _, _), etc

main :: IO ()
-- main is an IO action returning unit [called ()]
main = mapM_ (putStrLn . fizzBuzz) [1..100]
-- mapM_ action container
-- calls action on each element in the container
-- "putStrLn s" is the action of printing the string s
-- . composes two functions. (g . f) x = g (f x)
-- (putStrLn . fizzBuzz) = (\x -> putStrLn (fizzBuzz x))
-- [1..100] is a list of a numeric, enumerated type
-- Haskell defaults this to a list of Int, called [Int]
>>
>>58207924
Not all values have types that depend on the runtime. In fact, that pretty much seems to be an edge case, and not one that's very useful, either. In pretty much all of the real world cases, the compiler knows what the type of something is going to be, unless it's undecidable at compile time (but then again, Idris etc. aren't turing complete to begin with, so undecidable programs not compiling is hardly a big deal).

A comparison to Haskell would be to consider values with indeterminate runtime types to be the equivalent of existential types in Haskell - which can do the exact same thing (have a type that's determined at runtime)

However, existential types are usually frowned upon in Haskell for the exact same reason: Since you can't do anything with a value you don't know the type of, the only way to make them useful at all is to enclose some constraint into the existential, with which you can recover some operations. But if you have a type T that encloses a constraint giving you some operations, you can just represent your T as the sum of those operations instead.

That said, there's exactly one constraint that recovers some amount of power from existentials, and that's Typeable. Typeable+Existentials is pretty much dynamic typing in Haskell. It's not a common use case though, the only immediate example I can think of is the Exception system in Haskell.
>>
>>58209346
>>58209358
Good idea, that's a good example too
fmap :: (Functor f) => (a -> b) -> (f a -> f b)
fmap . fmap :: (Functor f, Functor g) => (a -> b) -> (g (f a) -> g (f b))
fmap . fmap . fmap :: (Functor f, Functor g, Functor h) => (a -> b) -> (h (g (f a)) -> h (g (f b)))
>>
>>58209446
>fmap . fmap . fmap
fmap (fmap fmap fmap) fmap
>>
>>58209455
>
fmap (fmap fmap fmap) fmap


fmap (fmap fmap fmap) . (fmap fmap fmap) . (fmap fmap fmap) fmap :: etc...
>>
>>58209455
>
fmap (fmap fmap fmap) fmap

Or just
fmap fmap fmap fmap fmap fmap fmap fmap

due to associativity
>>
>>58207073
The difference is extremely simply: Backtracking / reverse induction

In functional programming, you can always only lead from A to B. What's more, you always take the first available branch, and never lead back.

In logical programming, the interpreter can essentially “backtrack” evaluation to try out a different branch, and you can also apply functions in reverse (see what inputs may have produced a given output)

Basically, logical programming does two things:

1. disassociates the input and output of a function, and just turns them into arbitrary binary predicates instead.
2. implements a backtracking algorithm for finding solutions of equations of arbitrary binary predicates
>>
>>58209624 following this, let me highlight a crucial “oversight” you made in your example:

>>58207073
>
skin "Tyrone" x = {x = "Black"}

This is not syntax that makes sense in any functional language, because the concept of assigning an input variable makes no sense in any functional programming language.

The FP equivalent would be something like this:

skin "Tyrone" = Black
skin "Jamal" = Black
skin "Elsa" = White


Which in your logic programming example is
skin("Tyrone", Res) :- Res = black.
skin("Jamal", Res) :- Res = black.
skin("Elsa", Res) :- Res = white.


But crucially, that might as well be
skin(Res, "Tyrone") :- Res = black.
skin(Res, "Jamal") :- Res = black.
skin(Res, "Elsa") :- Res = white.


And heck, it's equivalent to this:
skin("Tyrone", black).
skin("Jamal", black).
skin("Elsa", white).


Now you can ask your logic interpreter stuff like
?- skin(X, black).


And it would give you back two solutions for X:

X = "Tyrone" ;
X = "Jamal".


This is impossible in an FP language, because an FP language cannot backtrack and cannot apply functions in reverse.
>>
>>58209732
Also, to see backtracking in action, you could extend your example like this:

criminal("Jamal").
criminal("Elsa").


and then ask it
?- skin(X, black), criminal(X).


And the only possible solution it would find for X is
"Jamal"
; because while it would try "Tyrone" first (based on the constraint for X given by
skin(X, black)
), that fails the
criminal
check, so it backtracks and tries the next possible value of X (
"Jamal"
) instead.
>>
>HaskLEL is useless and, pure functional and stateless languages are a meme
>Lisp is only used for third class text editors
>SICP is not a good book and this is why it's given away for free
>Anime is shit
>Maki is a slut
>C++ is much better than C
>Java is the language of the future
>JS is a good choice for server side
>The best software engineers are indians
>>
more like fap fap fap fap amirite guise XD
>>
>>58174033
>python


def tree_sixty():
print("Good Choice")
functional =True
return functional

def three_four_still_moar():
print("Error Finding Truing Completness")
funtional = False
return funtional

def two_by_two_hands_of_blue():
import sys
print("Fatal Error, Core Dumped!")
sys.exit()

def which_python(language):
case_switch= {
'3.6':tree_sixty,
'3.4':three_four_still_moar,
}
if language[6] == '3':
functional = case_switch[language[6:9]]()
elif language[6] == '2':
two_by_two_hands_of_blue()
else:
"Could Not Locate Item in Memory"
funtional = False
return functional

def is_functional(language):
if 'python' in language:
functional = which_python(language)
else:
"Sorry funtional == False"
functional = False
return functional

def main():
language = input("Please Enter Your Launguage\n>")
funtional = is_functional(language)
print("And The Question Is:\n Your Launguage Is Funtional\n What is,",funtional,"!")

main()
>>
>>58210179
What the hell am I reading
>>
File: SCR_1482945241.png (11KB, 232x182px) Image search: [Google]
SCR_1482945241.png
11KB, 232x182px
Is this a gross mistake or am I supposed to indent ifs like this in Haskell?
>>
>>58210356
It's a gross mistake because you could instead write this function like

mySignum x
| x < 0 = -1
| x > 0 = 1
| otherwise = 0
>>
>>58210356
I've never liked using ifs much. I prefer using guards / pattern matching if I can
mySignum 0 = 0
mySignum x
| x < 0 = -1
| otherwise = 1
>>
>>58210356
My rule of thumb is that if you have an indentation chain like that you need to restructure your code instead

Most of the time, any sort of chain like that indicates that you could be using a monad instead; although in this case a guard is even cleaner
>>
>>58210356
id have to wonder why that else if is indented past the if
>>
>>58210395
>>58210401

Yeah, I know guards are cool and all. I was just wondering if that mess was somehow not a bad thing.
>>
>>58210411
In Haskell, ‘then’ and ‘else’ are both sub-structures associated with the ‘if’. The indentation of that example was meant to demonstrate the syntactical structure.
>>
>>58210429
>I was just wondering if that mess was somehow not a bad thing.
No, it's absolutely terrible and code like that deserves to be shot, not shown to beginners
>>
>>58210356
{-# LANGUAGE MultiWayIf #-}

mySignum x =
if | x < 0 -> -1
| x == 0 -> 0
| x > 0 -> 1
>>
>>58210485
WHY
>>
>>58210499
ift True t _ = t
ift False _ f = f

mySignum x =
ift (x<0) (-1)
(ift (x>0) 1 0)
>>
>>58210499
{-# LANGUAGE RebindableSyntax #-}
import Prelude

ifThenElse x _ _ = signum x
mySignum x = if x then x else x
>>
>>58210434
is the indentation part of haskell? could he have wrote the else if on the same line as the if
>>
>>58210836
yes he could write it all on one line
>>
>>58210555
What a hilariously impractical way of writing
mySignum = signum
>>
>>58210836
>is the indentation part of haskell?
Indentation is normally meaningful in Haskell, but in this case (if then else) it doesn't matter because they don't introduce a new block, they're still part of the same expression
>>
File: coqawaii.png (276KB, 650x600px) Image search: [Google]
coqawaii.png
276KB, 650x600px
bump
>>
new
>>58212017
Thread posts: 328
Thread images: 28


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.