/fpt/ - Functional Programming Thread

Next-level programming thread.
Try it, you'll like it!

Last one: Resources:
>Elixir
chimera.labs.oreilly.com/books/1234000001642/index.html
>Elm
guide.elm-lang.org/
>Erlang
learnyousomeerlang.com/content
>F#
fsharp.org/learn
>Haskell
en.wikibooks.org/wiki/Haskell
>Lisps
>>Clojure
braveclojure.com/foreword/
>>Scheme
SICP

Feel free to add more.

Other urls found in this thread:

boards.Sup
gigamonkeys.com/book/),
0x0.st/9y4.hs
twitter.com/NSFWRedditVideo

Does anybody ever actually ask Haskell questions in these threads or is it pure shitposting and memes?

the fuck is this shit
we already have a

>Enter thread
>See Elixir
>See Elm
>See Erlang
>Leave thread

Bye.

Pure and immutable shitposting and memes.

Last thread we had:
>2 posts with blocks of Clojure code
>1 post with psuedocode

And that was it.

also, next time you create a general, make sure you put general in the subject so the filters can work
boards.Sup Forums.org/g/catalog#s=dpt|wdg|sqt|general

I guess complaining won't help so: /fpt/, what's your favorite trick or tool in your language of choice?

/dpt/ doesn't have General in subject.

because I already know that there's a /dpt/
>implying i want to scour the entire catalog everytime
sage

>favorite trick
using the codensity monad to improve any free monad construction by right-associating all binds via use of the yoneda isomorphism

Kill yourself you worthless triple bigger

What fucking difference does it make to you that this thread exists

Who is this newfag trying to tell people what they are allowed to post?

Kill yourself underage faggot


Has anybody experience with Scala? Is it harder or easier than Haskell?

Scala is harder to learn functional principles in because it is also fully oop. It also has pretty much every language feature ever. People don't actually know that Scala is technically a fully fledged dynamic Lang. You just have to add an import (it's heavily frowned upon to do this mind). The plus side is that it is more pragmatic and there are more jobs.

Haskell is more sound and will teach you fp easier (as you can't back out) but can be really unforgiving if you don't understand certain concepts.

They are both fine Langs.

Feel free to

I want to answer Haskell questions, not ask them - I know a lot about Haskell already, and can't think of any open questions I'd have

I'm new to functional field, so overall concepts of FP do amuse me.

When will this meme die?

>one will land a good job, but you will be called pajeet forever
>the other will not land you a job, and you will be called hipster faggot forever

What's the best basic math book?

...

Too advanced, i can't even add yet

Scala Circe.

JSON serialisation and deserialisation in a completely type safe way. Code generated at compile time , no reflection or macros.

All you need to do is an import.

Get out of here, you reddit tourist.

Haskell has Generics for this, and Aeson does JSON

{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)
import Data.Aeson (ToJSON, FromJSON)

data MyDataType ... ... ...
deriving (Generic)

instance ToJSON MyDataType
instance FromJSON MyDataType
-- no implementation needed

This is how it's implemented in Scala as far as I know.

Haskell has classes.
What went wrong?

I'm currently learning Haskell as my first programming language (not strictly true, I programmed when I was younger and dabbled in things ranging from C++ to our old favorite, Visual basic 2006, but it has been long enough that I only know programming concepts and not languages anymore) and am enjoying it so far, but I'm wondering if it is the best functional programming language to learn, or if there is one that has significant advantages over it for general use and better lasting impact on me in terms of teaching me programming skills that will translate into other languages.

For reference it won't have much application with my job, and will mostly be a resume booster and fun hobby.

What do you mean?

just started learning haskell today
how to fix this?
import System.Random

comp n rand =
if n < rand
then print "too small"
else if n > rand
then print "too big"
else return ()

main = do
xxx

He means type classes without actually knowing they are completely different from oop classes.

comp takes a regular integer (or anything that can be ordered)

readLn isn't an int, it's an IO int
So you need to "extract" the int in do-notation

n

>le useless academic meme thread

Next we'll have an esoteric programming thread.

Sage, hide and report.

This isn't even the worse bait I've seen today

You mean this thread?

No, I mean the part where you said you had a job, a life and a girlfriend.

My favourite functional programming language is JavaScript.

If you call that college undergrad research grant a "job", fine.

Ha ha very funny. You can go now.

So can you. This board is about technology, not fringe academic theories that have no practical purposes. Technology implies practicality.

No, honestly, you aren't funny.
You're just spamming and shitposting.
Fuck off already.

Seconding this. I want to make sure Haskell is the one worth getting into, and if not, what is?

comp n rand =
if n < rand
then print "too small"
else if n > rand
then print "too big"
else return ()


guard syntax would be much better for this kind of multi-way-if construction

comp n rand
| n < rand = putStrLn "too small"
| n > rand = putStrLn "too big"
| otherwise = return ()


Or another way you could do this is via pattern matching, e.g.

main = do
target putStrLn "too big"
EQ -> return ()

Pretty sure you meant to quote instead.

This thread is cancer. Sage and report.

Please stop posting this shit.

Nobody cares about muh functional meme.

What's the deal with functional programming?

It's not programming and it's not functional!

>functional programming

That's a funny way to say "esoteric programming".

Any good Common Lisp books?
Currently reading Practical Common Lisp (gigamonkeys.com/book/), but I wonder if there are any better alternatives...

Please take a screenshot of this post, I want to see how many (You)s there are

No, leave Sup Forums.

Sure. Here you go.

Stop.

Go away already, OP. You're not fooling anyone by samefagging in your own thread.

Fuck off.

What psyop tactic did I accidentally stumble into here?

I mean the goal is pretty clear, but I wonder what the motivation is? Who would possibly gain anything from censoring knowledge? Presumably, people who want to prevent it from spreading.

The most astonishing thing is that it's not just one guy, it seems to be some form of collective effort at the very least. Has Oracle started hiring people to ensure Java job security by preventing progress? Seems a bit weird to be doing that on Sup Forums, of all places.

Another possible motive would be somebody bitter and angry that he couldn't figure something out, trying to eliminate all mentions of it / belittle it to make himself feel better about it; but that wouldn't explain the high degree of organization.

Anybody else have an idea? Genuinely interested now

Love you too guys!
Not OP, by the way

Have you programmed in Mathematica today?

It's literally just someone with autism who thinks he's funny.
He was probably bullied as a child. Sad, really.

been dicking around with clojure recently (rorfag here. yeah yeah but it pays the rent and leaves me with an extra 8 hours a day for jerking off)

anyway I got the joy of clojure since I like that guy's blog and honestly I think it's one of the only "how to $LANG" book I've liked

it goes fast and assumes you know how to google, but it has been pretty useful for coming up to speed

anyway if you can't stand most intro books but want a nice tour I recommend it

thanks, so I have this. Is there something I should change or this is acceptable?

import System.Random
import Control.Monad.Cont

comp n rand
| n < rand = print "too small"
| n > rand = print "too big"
| otherwise = print "yep"

main = do
xxx do
a

If you don't care about the number (or the result), you can use
replicateM_
from Control.Monad (it might be in Cont)

If you want to do it forever, you can use
forever
(also from Control.Monad)
e.g.
forever $ do
a rand = "too big"
| otherwise = "yep"

and then

print (comp a xxx)

1. ‘print’ does not do what you think it does. The function for printing a string to stdout is putStr / putStrLn. ‘print’ is defined as print = putStrLn . show, which for strings means you'd end up with a source-code representation (including quotes and escaping)

2. If you're going to be printing the result in all cases, you could just do something like this:

comp n rand = print $ case compare n rand of
LT -> "too small"
GT -> "too big"
EQ -> "yep"


3. you could swap comp's arguments and use something like forever $ readLn >>= comp

>forever $ readLn >>= comp xxx
fixed

>I'm bullied even on Sup Forums

bump

What would be the best way to implement finite states machine in Haskell ?

Am I an idiot if I tough of that ? :
data States = States [Int]
data Transitions = Transitions [(Int, Char, Int)]
data InitStates = InitStates [Int]
data FinalStates = FinalStates [Int]

data Automate = Automate (States, Transitions, InitStates, FinalStates)

Depends how crazy you wanna go

I'm looking for something basic/naive.

And shit this is some trve power level here

you can represent the states as a data type and the state transitions as functions

I just though about the transitions as functions yeah.

Shit gets messy with tuples

F# active patterns are a super-useful construct. It's nice to be able to generate my own pattern-matching forms and have that act-on-data-then-match it form. Definitely feels like an F# "killer feature".

active patterns are good and it would be nice to see higher order patterns in haskell

roll and post your results in your favorite functional language

Here's a simple DFA implementation I made as part of a DFA->regex thing

data DFA = DFA { alphabet :: [Char], states :: Int, start :: Int, finals :: [Int], fun :: Int -> Char -> Int }

checkD :: DFA -> String -> Bool
checkD d s = go (start d) s `elem` finals d
where go !n [] = n
go !n (x:xs) = go (fun d n x) xs


Use it like this:

goal :: DFA
goal = DFA { alphabet = "01", states = 4, start = 1, finals = [1], fun = f }
where f n '0' = case n of 1 -> 2; 2 -> 1; 3 -> 4; 4 -> 3
f n '1' = case n of 1 -> 3; 3 -> 1; 2 -> 4; 4 -> 2

test :: DFA
test = DFA { alphabet = "01", states = 2, start = 1, finals = [2], fun = f }
where f 1 _ = 2
f 2 _ = 1


Here's the full code, if you're interested: 0x0.st/9y4.hs

It's not 100% correct, there are still some bugs in my O(n*m) regex matching algorithm. (I had an exponential version which was correct, but too slow). Can't be bothered to waste more time on it though.

Can you provide a quick example in F#? I want to see how close I can get in Haskell

in Haskell you'd use ViewPatterns or PatternSynonyms

Tell me why i should switch over from c++

C++ is one of the worst languages ever designed

Oh and it's gradually trying to adopt FP features but it's far, FAR too late

Yeah but I mean, what do active patterns provide on top of ViewPatterns + PatternSynonyms?

c++ is a huge mess,
it has a ton of oddities and quirks, and hacks to get around them.

and it will only get worse as it tries to add new features while keeping backwards compatibility (aka mistakes)

ViewPatterns + PatternSynonyms are pretty much equal to active patterns

But there are things Haskell is missing, e.g.
or-patterns:

case x of
0 || 1 -> "got 0 or 1"
-- trivial example
a 'c' x || b 0 x False -> show x
-- bind the same variables, so we can combine them

and-patterns

case x of
(0, y) & (_, (_, z, _))
(in this case you can use an as pattern)

as patterns are generally a subset of and-patterns

lifting a -> Maybe b
case list of
safeHead x -> ...
(use viewpatterns)

case list of
safeHead -> Just x

patterns are kind of second class citizens and guards are another example

If C++ drops C heritage, it would be better.

If you want to have patterns as a first-class-citizen in Haskell, you should look into Prisms (from the lens library). There's sadly no built-in syntax for them, but you can define whatever prism you want and then wrap a pattern synonym around it.

Prisms can trivially do or/and/view patterns, as well as stuff like prism composition.

if languages don't dress up as C '''CS majors''' can't understand them

C++ is never going to be good

What's a less retarded way of doing this? I'm just starting out.
operation :: String -> Float -> Float -> Float
operation op x y -- op, x and y are user input
| op == "+" = doAdd x y
| op == "-" = doSub x y
| op == "/" = doDiv x y
| op == "*" = doMul x y
| otherwise = 0

for example, the problem im having right now is its "uniform" initialization syntax being not very uniform at all.
so i have to figure out all sorts of exception cases to get it to work properly.

its C stuff is mostly fine,
the biggest problem it gets from it is headers. hopefully we get to use modules soon.

Pattern matching

operation :: String -> Float -> Float -> Float
operation "+" = doAdd
operation "-" = doSub
operation "*" = doMul
operation _ = 0

Or perhaps even something like this (for less repetition)

operation = \case
"+" -> doAdd
"-" -> doSub
"*" -> doMul
_ -> 0

>as C
You wanted to say "as Algol".

I am a program engineering student and we don't see functional programming, but from what i read functional programming is all about having no state

Land of Lisp was really good. By the time you get to the end of the book, you have done something actually kind of crazy, and you don't realize what it is until you get 90% of the way there. (Which is why I am not specifying what "it" is, because it's a really cool realization.)

It's also a fun book.

I highly recommend it.

Jesus, thank you user

Puzzle-oriented programming

Thanks user, I'll check it out

Should I get better at imperative programming in general before learning something like Haskell? The only thing I've done so far is C.

You should do whatever interests you and you enjoy doing.

roll

Hmm, I'm undecided as to how much effort I want to put into this

From easy to difficult:
>trivially embedding data into the low bits of a lossless file format
>making the embedded data match a real-world noise distribution more accurately
>embedding data into lossy images (e.g. JPEG) by perturbing the highest-frequency DCT bits, then recompressing the result losslessly

if singles first
if dubs second
if trips third