[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Functional Programming
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 112
Thread images: 6
File: lambda.png (2 KB, 175x237) Image search: [Google]
lambda.png
2 KB, 175x237
What's your favorite functional programming language?
>>
Urbit :>)
>>
I only learned haskell so...
>>
D
>>
Perl
>>
>>54831672
APL
>>
>Useful; serving a purpose, fulfilling a function
>Designed to be practical and useful, rather than attractive
>Working or operating

C
>>
How do you write FizzBuzz in a functional programming language?
>>
Scheme
>>
>>54831672
>The lambda was selected as a symbol by the Gay Activists Alliance of New York in 1970, and declared the international symbol for gay and lesbian rights by the International Gay Rights Congress in Edinburgh, Scotland, in 1974.
https://en.wikipedia.org/wiki/Lambda
OP confirmed for faggotry again.
>>
Prolog
>>
File: iDontCare.webm (631 KB, 624x352) Image search: [Google]
iDontCare.webm
631 KB, 624x352
>>54832664
>>
>>54832484
easy
>>
>>54831672
Scheme!
>>
>>54833690
>How
As in "how would someone", I'm having trouble wrapping my head around FP for some reason.
>>
>>54833950
here is a way which looks familiar (in f#)
 [1..100] |> List.iter (fun i ->
match i with
| i when i % 15 = 0 -> printf "FizzBuzz, "
| i when i % 3 = 0 -> printf "Fizz, "
| i when i % 5 = 0 -> printf "Buzz, "
| _ -> printf "%d, " i
)


here is something more "functional" (in Haskell)
let (m ~> str) x = str <$ guard (x `mod` m == 0)
in map (fromMaybe . show <*> 3 ~> "fizz" <> 5 ~> "buzz")
>>
>>54834103
broken down
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Control.Monad (guard)
import Control.Applicative ((<$), (<*>))

-- This function should be pretty straightforward
m `divides` x = x `mod` m == 0

(~>) :: Integer -> String -> Integer -> Maybe String
(m ~> str) x = str <$ guard (m `divides` x)

-- A more readable version of this function would read like this
-- (m ~> str) x = if m `divides` x
-- then Just str
-- else Nothing

-- Taking advantage of Monoid instances for functions, Maybe and String (holy god!)
fizzBuzzOrNeither :: Integer -> Maybe String
fizzBuzzOrNeither = 3 ~> "fizz" <> 5 ~> "buzz"

-- Taking advantage of the Applicative instance for functions (I am pretty sure that's what's going on)
integerToFizzBuzzString :: Integer -> String
integerToFizzBuzzString = fromMaybe . show <*> fizzBuzzOrNeither

result = map integerToFizzBuzzString [1..]
>>
>>54832484
f(N) when N rem 15 == 0 ->
fizzbuzz;
f(N) when N rem 5 == 0 ->
buzz;
f(N) when N rem 3 == 0 ->
fizz;
f(N) ->
N.

fizzbuzz() ->
[f(N) || N <- lists:seq(1,100)].

fbspawn() ->
fbspawn(9000).

fbspawn(N) when N =:= 0 ->
spawn(fun() -> fizzbuzz() end );
fbspawn(N) ->
spawn(fun() -> fizzbuzz() end ),
fbspawn(N-1).
>>
>>54834103
>>54834130
>>54834222
What's the advantage of FP anyway? Less bugs presumably since you are staying away from mutating data, but isn't there far more calls and memory usage instead?
>>
>>54834470
Like anything that's heavily abstracted, FP suffers from hidden costs caused by certain abstractions.
Also, the eco systems of pretty much every single FP language greatly suffer from overengineering and premature abstraction.
>>
>>54834470
- Personally, I find certain types of applications much easier to write in this format. That's not a universal truth, I've tried to replace some short shells scripts in Erlang and it's PITA.
- I can run this code on a 256MB Raspberry Pi. Java, which is not functional, would die in a fire in that sort of environment. I don't think memory use is related.
>>
>>54834222
Hyperthreading Fizzbuzz!
>>
>>54834503
FP has no side effects, so it doesn't suffer anything from abstractions

it's actually a big advantage
>>
>>54834470
concise code, less bugs, etc.
You can have a read of this
https://fsharpforfunandprofit.com/why-use-fsharp/
>>
>>54834578
You seriously think abstraction can only have disadvantages when side effects are involved?

I think I'm done with this place. How people can make fun of stateless factories in Java while still rooting for abstracting every single thing for maximum DRYness in functional languages is beyond me.
>>
>>54834643
because java produces boilerplate and isn't pure, that simple
>>
>>54834655
Stateless factories are pure.
What do you think redundant and premature abstraction is in Haskell? No boilerplate?
>>
>>54832484
whichfizz = fn
(0, 0, _) -> "FizzBuzz"
(0, _, _) -> "Fizz"
(_, 0, _) -> "Buzz"
(_, _, n) -> n
end

fizzbuzz = fn (n) ->
whichfizz.(rem(n, 3), rem(n, 5), n)
end

IO.inspect Enum.map(1..100, fizzbuzz)
>>
>>54834683
due to the nature of haskell, you do not need to redefine everything like in Java
so no boilerplate
look up monads, combinators, functors, etc.
>>
>>54834710
I do not know what those are and what they're useful for.

I'm seriously afraid of FP becoming mainstream, it'll be the same clusterfuck OOP caused because people cargo cult the shit they're taught in uni without thinking about the disadvantages of applying their "silver bullet" everywhere.
>>
>>54831672

clojure, but i want to learn more about common lisp
>>
Elixir, Clojure, Haskell, Lisp.

Probably in that order too.
>>
File: 1464248392605.jpg (12 KB, 236x236) Image search: [Google]
1464248392605.jpg
12 KB, 236x236
>>54834643
You mean you don't enjoy reading LZW-compressed source code, anon? Y U no l33t FP?
>>
Who here studies category theory? How are Hask objects different than Set objects in math?
>>
File: 1432414065231.jpg (165 KB, 792x651) Image search: [Google]
1432414065231.jpg
165 KB, 792x651
Can you do Functional Programing in C/C++?
>>
>>54832664
Didn't fags also claimed all the colours?
>>
>>54835107
C++ yes somewhat, not C
>>
>>54835176
Do you mind expanding that a bit? This shit is doing my head in, i think i get the core concepts, but all the actual examples are in languages I've never touched.
>>
>>54835176
You can do FP in any Turing complete language. It's just that some are more difficult than others, and may involve first implementing a lisp interpreter.
>>
>>54834578
Under the hood those languages aren't pure FP since they run on procedural machines. The language they compile to are rarely sode effect free.
>>
How do you do bfs neatly in fp I've tried and it comes out hideously...
>>
>>54834643

It's a different kind of abstraction.

What you're talking about is indirection.
>>
>>54832621
>>54833787
Scheme
>>
>>54831672
Haskell
>>
>>54831672
Hasklel, because I like my functional languages pure.
>>
>>54832484
naive version

fizzbuzz :: Integer -> String
fizzbuzz n = case (n `rem` 3, n `rem` 5) of
(0, 0) -> "fizzbuzz"
(0, _) -> "fizz"
(_, 0) -> "buzz"
(_, _) -> show n

main = mapM_ (putStrLn . fizzbuzz) [0..100]
>>
>>54834103
>>54834130
https://github.com/haasn/fizzbuzz/tree/master
>>
>>54831672
F#
>>
>>54834702
Elixir fag detected
>>
>>54835366
>bfs
breadth-first search?

here's my naive, inefficient, zero-th order attempt

data Tree a = Empty | Bin (Tree a) a (Tree a)

enumB :: Tree a -> [a]
enumB = concat . go
where go Empty = []
go (Bin l x r) = [x] : merge (go l) (go r)

merge (x:xs) (y:ys) = (x++y) : merge xs ys
merge xs [] = xs
merge [] ys = ys
>>
>>54835585
WTF I like haasn now
>>
>>54835680
second-order “optimization”

enumB' :: Tree a -> [a]
enumB' t = go [t]
where go [] = []
go (Empty : ts) = go ts
go (Bin l x r : ts) = x : go (ts++[l,r])


still O(n^2) due to the use of linked lists as a substitute for a queue, but this is easily improved on
>>
>>54835865
third order optimization (with an amortized O(1) queue), plus checks and benchmarks

{-# LANGUAGE DeriveGeneric, BangPatterns #-}

import Control.Applicative (liftA3)
import GHC.Generics (Generic)
import Control.DeepSeq (NFData, force)

import Test.QuickCheck
import Criterion
import qualified Criterion.Main

data Tree a = Empty | Bin (Tree a) a (Tree a)
deriving (Show, Generic)

instance CoArbitrary a => CoArbitrary (Tree a)
instance Arbitrary a => Arbitrary (Tree a) where
arbitrary = oneof [pure Empty, liftA3 Bin arbitrary arbitrary arbitrary]
shrink = genericShrink

instance NFData a => NFData (Tree a)

enumB1 :: Tree a -> [a]
enumB1 = concat . go
where go Empty = []
go (Bin l x r) = [x] : merge (go l) (go r)

merge (x:xs) (y:ys) = (x++y) : merge xs ys
merge xs [] = xs
merge [] ys = ys

enumB2 :: Tree a -> [a]
enumB2 t = go [t]
where go [] = []
go (Empty : ts) = go ts
go (Bin l x r : ts) = x : go (ts++[l,r])

enumB3 :: Tree a -> [a]
enumB3 t = go [] [t]
where go [] [] = []
go rs [] = go [] (reverse rs)
go rs (t:ts) = case t of
Empty -> go rs ts
Bin l x r -> x : go (r:l:rs) ts


-- Test
propB12 x = enumB1 x == enumB2 (x :: Tree Int)
propB23 x = enumB2 x == enumB3 (x :: Tree Int)

-- Benchmark
benchB123 =
[ bench "enumB1" $ whnf (length . enumB1) tree
, bench "enumB2" $ whnf (length . enumB2) tree
, bench "enumB3" $ whnf (length . enumB3) tree
] where !tree = force $ iterate (\t -> Bin t () t) Empty !! 8

main = do
quickCheck propB12
quickCheck propB23
Criterion.Main.defaultMain benchB123
>>
>>54836191
λ ghc -O2 bfs.hs -o ~/tmp/bfs && ~/tmp/bfs
+++ OK, passed 100 tests.
+++ OK, passed 100 tests.
benchmarking enumB1
time 16.70 μs (16.62 μs .. 16.81 μs)
1.000 R2 (0.999 R2 .. 1.000 R2)
mean 16.75 μs (16.68 μs .. 16.89 μs)
std dev 341.6 ns (206.2 ns .. 609.2 ns)
variance introduced by outliers: 19% (moderately inflated)

benchmarking enumB2
time 195.0 μs (193.6 μs .. 196.4 μs)
0.999 R2 (0.999 R2 .. 1.000 R2)
mean 196.8 μs (195.2 μs .. 201.8 μs)
std dev 9.253 μs (2.646 μs .. 19.19 μs)
variance introduced by outliers: 46% (moderately inflated)

benchmarking enumB3
time 5.631 μs (5.603 μs .. 5.663 μs)
1.000 R2 (1.000 R2 .. 1.000 R2)
mean 5.630 μs (5.611 μs .. 5.656 μs)
std dev 76.19 ns (51.62 ns .. 113.6 ns)
variance introduced by outliers: 11% (moderately inflated)
>>
>>54836202
Also forgot to mention, all of my implementations are both lazy and streaming (as opposed to some of the others you'll find on the internet)
>>
>>54831672
Probably S-BASIC.
>>
>>54836191
Generalized to an arbitrary fold

foldB :: Monoid a => Tree a -> a
foldB t = go [] [t]
where go [] [] = mempty
go rs [] = go [] (reverse rs)
go rs (t:ts) = case t of
Empty -> go rs ts
Bin l x r -> x <> go (r:l:rs) ts
>>
>>54835012
Hask is different from Set because of non-termination/undefined.
>>
>>54834749
>I don't know the first thing about FP but I just KNOW it's a failure like OOP
Could have just started by saying you're an autistic C programmer.
>>
>>54831672
My favorite one is Haskell. Currently playing with some Clojure.
>>
OCaml and Elixir
>>
>Half Life symbol
>>>/v/
>>
>>54831672
c++
>>
How come nobody has mentioned real functinoal programming languages like Idris or Coq or F*?
>>
>>54840442
>Idris
Nobody uses it. Not real-world ready

>Coq
Isn't really a programming language

>F*
Nobody uses it. Not real-world ready. Microsoft
>>
Racket.
>>
>>Idris
>Nobody uses it. Not real-world ready

Give it time anon, give it time.

>>Coq
>Isn't really a programming language

If it isn't, then what is it then?
>>
>>54840663
>If it isn't, then what is it then?
Theorem proving assistant
>>
>>54840674
I see you can't into Curry-Howard.
>>
>>54840442
Because people want their programs to do things.
>>
File: maxresdefault.jpg (87 KB, 1280x720) Image search: [Google]
maxresdefault.jpg
87 KB, 1280x720
javascript

>i'll see myself out
>>
>>54840709
I see you've never tried writing a real-world program with Coq
>>
>>54840809
Why not have programs that do things and be bug free at the same time?
>>
>>54840853
If by "real-world program", you mean CRUD apps, then no. If you mean compilers, then yes: http://compcert.inria.fr/download.html.
>>
>>54840863
>be bug free
What does that have to do with functional programming?

In Haskell, you can make a program hang by reading a list or any other data structure.

Coq has bugs that let you "prove" false.
>>
>>54835168
no, just ugly, incorrect rainbows
>>
File: compcert.png (7 KB, 541x330) Image search: [Google]
compcert.png
7 KB, 541x330
>>54840912
wew lad
>>
>>54840962
>Coq has bugs that let you "prove" false.

I would like to see one of the bugs. In any case, you can always "admit" the proof of anything and prove anything you want, even false. Also, you don't have to prove anything if you don't want to, but that road leads to bugland.
>>
>>54840962
>In Haskell, you can make a program hang by reading a list or any other data structure

How do you read a list in Haskell? That makes no sense to me.
>>
>>54841180
Pattern matching. Maybe "observing" is a better word to use.
>>
>>54840863
1. bug free programming is a myth
2. static verification is only as good as the properties you choose to statically verify
3. real-world programming languages need to solve complex real-world issues with big complicated runtime systems for stuff like asynchronous threaded I/O managers. most of these meme languages hide all of the real complexity inside the RTS
>>
>>54841225
>real-world programming languages need to solve complex real-world issues with big complicated runtime systems for stuff like asynchronous threaded I/O managers

The only real issue is using a shitty programming language because "hur durr, everybody else uses it anon and I need a job".

Stay pleb anon.
>>
>>54832078
C is an imperative language.
>>
>>54841365
>The only real issue is using a shitty programming language because "hur durr, everybody else uses it anon and I need a job".
So you basically don't have a reply. Okay then, I'll keep on using Haskell
>>
>>54831672
Once Haskell gets refinement types, it will be my favorite programming language ever. I hope that Liquid Haskell stuff gains more momentum.
>>
>>54841596
For me it's once Haskell gets dependent types

(real dependent types, none of this type family singleton datakinds hocus pocus)
>>
>>54841596
Are there any languages that have real refinement types? I think F* has them.
>>
>>54841863
Algol Y was going to.
>>
>>54841863
I believe Ada has refinement types to a limited extent (e.g. defining subtypes of integers).
>>
>>54831680
Hello Curtis (^:

How is that Neo-Reaction thing going?
>>
what is functional programming
>>
>>54842664
Programming that is
>Useful; serving a purpose, fulfilling a function
>Designed to be practical and useful, rather than attractive
>Working or operating
>>
Since Functional Programming is the hot shit now, which is the most popular FP language right now, ie the most likely to get me a job?
>>
>>54842796
Haskell or OCaml, but no one want to pay you for doing Haskell or OCaml.
>>
>>54842838
What about one of the F languages?
>>
>>54842912
The what?
>>
>>54842928
F/F*/F#
>>
What do you gain by doing functional programming vs any other paradigm?
>>
>>54842971
tail call optimization
>>
>>54842986
Algol 60 and BLISS supported tail call optimization.
>>
>>54841977
Ada 2012 has them completely. Preconditions, postconditions and type invariants are pretty great.
>>
>>54842986
What's that?
>>
>>54843151
google it
>>
>>54843151
probably that it doesn't keep a stack and that the function is strict (aka the function evaluates is arguments before using them)
>>
>>54842838
>no one would pay you for doing ocaml
is this true
>>
>>54831788
dead language f⍺m
>>
>>54842986
TCO doesn't have anything to do in particular with functional programming. Non-functional language compilers can implement it.

Getting things into tail call form for real-world problems is usually wildly obtuse and hard to read compared to a simple for-loop. Yeah, yeah, it mutates, but it doesn't fucking matter if it's inside a tiny function. Global mutation is the devil. Mutation in small functions rarely causes difficult bugs.
>>
>>54845229
Forgot to say, making things tail recursive is often difficult and makes things hard to read for you and other people. If you fuck up, which is at least as easy as a bug from mutation, you got stack overflow. It's not any better.

Programming is first and foremost to solve real-world problems. Avoiding spaghetti code, uncontrolled mutation, and preferring map/filter/etc to loops is great. But dogmatic insistence on NO MUTATION EVER! is retarded.

At any rate, you can make all the same mutation mistakes in Haskell that you can anywhere else. Being in a monad instead of just a function doesn't magically make it not a problem.
>>
>>54840442
F* is for verification, it doesn't produce machine code
>>
>>54842796
Scala has most jobs
then clojure and F#
there are a few haskell ones as well
>>
Emacs Lisp ;)
>>
>>54831672
Fucking meme language
Thread replies: 112
Thread images: 6

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.