Functional Programming

What's your favorite functional programming language?

Other urls found in this thread:

en.wikipedia.org/wiki/Lambda
fsharpforfunandprofit.com/why-use-fsharp/
github.com/haasn/fizzbuzz/tree/master
compcert.inria.fr/download.html.
twitter.com/AnonBabble

Urbit :>)

I only learned haskell so...

D

Perl

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

>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.
en.wikipedia.org/wiki/Lambda
OP confirmed for faggotry again.

Prolog

...

easy

Scheme!

>How
As in "how would someone", I'm having trouble wrapping my head around FP for some reason.

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 "fizz" 5 ~> "buzz")

broken down
import Data.Maybe (fromMaybe)
import Data.Monoid (())
import Control.Monad (guard)
import Control.Applicative (() :: Integer -> String -> Integer -> Maybe String
(m ~> str) x = str 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..]

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
fbspawn(9000).

fbspawn(N) when N =:= 0 ->
spawn(fun() -> fizzbuzz() end );
fbspawn(N) ->
spawn(fun() -> fizzbuzz() end ),
fbspawn(N-1).

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?

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.

- 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.

Hyperthreading Fizzbuzz!

FP has no side effects, so it doesn't suffer anything from abstractions

it's actually a big advantage

concise code, less bugs, etc.
You can have a read of this
fsharpforfunandprofit.com/why-use-fsharp/

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.

because java produces boilerplate and isn't pure, that simple

Stateless factories are pure.
What do you think redundant and premature abstraction is in Haskell? No boilerplate?

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)

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.

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.

clojure, but i want to learn more about common lisp

Elixir, Clojure, Haskell, Lisp.

Probably in that order too.

You mean you don't enjoy reading LZW-compressed source code, user? Y U no l33t FP?

Who here studies category theory? How are Hask objects different than Set objects in math?

Can you do Functional Programing in C/C++?

Didn't fags also claimed all the colours?

C++ yes somewhat, not C

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.

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.

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...

It's a different kind of abstraction.

What you're talking about is indirection.

Scheme

Haskell

Hasklel, because I like my functional languages pure.

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]

github.com/haasn/fizzbuzz/tree/master

F#

Elixir fag detected

>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

WTF I like haasn now

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

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

λ 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)

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)

Probably S-BASIC.

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

Hask is different from Set because of non-termination/undefined.

>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.

My favorite one is Haskell. Currently playing with some Clojure.

OCaml and Elixir

>Half Life symbol

c++

How come nobody has mentioned real functinoal programming languages like Idris or Coq or F*?

>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 user, give it time.

>>Coq
>Isn't really a programming language

If it isn't, then what is it then?

>If it isn't, then what is it then?
Theorem proving assistant

I see you can't into Curry-Howard.

Because people want their programs to do things.

javascript

>i'll see myself out

I see you've never tried writing a real-world program with Coq

Why not have programs that do things and be bug free at the same time?

If by "real-world program", you mean CRUD apps, then no. If you mean compilers, then yes: compcert.inria.fr/download.html.

>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.

no, just ugly, incorrect rainbows

wew lad

>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.

>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.

Pattern matching. Maybe "observing" is a better word to use.

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

>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 user and I need a job".

Stay pleb user.

C is an imperative language.

>The only real issue is using a shitty programming language because "hur durr, everybody else uses it user and I need a job".
So you basically don't have a reply. Okay then, I'll keep on using Haskell

Once Haskell gets refinement types, it will be my favorite programming language ever. I hope that Liquid Haskell stuff gains more momentum.

For me it's once Haskell gets dependent types

(real dependent types, none of this type family singleton datakinds hocus pocus)

Are there any languages that have real refinement types? I think F* has them.

Algol Y was going to.

I believe Ada has refinement types to a limited extent (e.g. defining subtypes of integers).

Hello Curtis (^:

How is that Neo-Reaction thing going?

what is functional programming

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?

Haskell or OCaml, but no one want to pay you for doing Haskell or OCaml.

What about one of the F languages?

The what?

F/F*/F#

What do you gain by doing functional programming vs any other paradigm?

tail call optimization

Algol 60 and BLISS supported tail call optimization.