Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
distortion park
Apr 25, 2011


AlsoD posted:

you did pick the example in which two nested binary trees are probed, i don't think there's any language where needing to account for 12 or so different cases would be anything other than a whole load of code.


everybody says the same thing and it will come in time, it helps that you can never have pattern matching in the same place as function application and vice versa - pattern matching always is in a function definition (between its name and "=") or in a lambda (between "\" and "->")

i don't find writing it so bad and it's kinda cool to solve small problems with it. imo it's a really extreme case of 'code is twice as hard to read as write' or however it goes.

Adbot
ADBOT LOVES YOU

Bloody
Mar 3, 2013

pointsofdata posted:

code:
balanceL :: a -> Set a -> Set a -> Set a
balanceL x l r = case r of
  Tip -> case l of
           Tip -> Bin 1 x Tip Tip
           (Bin _ _ Tip Tip) -> Bin 2 x l Tip
           (Bin _ lx Tip (Bin _ lrx _ _)) -> Bin 3 lrx (Bin 1 lx Tip Tip) (Bin 1 x Tip Tip)
           (Bin _ lx ll@(Bin _ _ _ _) Tip) -> Bin 3 lx ll (Bin 1 x Tip Tip)
           (Bin ls lx ll@(Bin lls _ _ _) lr@(Bin lrs lrx lrl lrr))
             | lrs < ratio*lls -> Bin (1+ls) lx ll (Bin (1+lrs) x lr Tip)
             | otherwise -> Bin (1+ls) lrx (Bin (1+lls+size lrl) lx ll lrl) (Bin (1+size lrr) x lrr Tip)

  (Bin rs _ _ _) -> case l of
           Tip -> Bin (1+rs) x Tip r

           (Bin ls lx ll lr)
              | ls > delta*rs  -> case (ll, lr) of
                   (Bin lls _ _ _, Bin lrs lrx lrl lrr)
                     | lrs < ratio*lls -> Bin (1+ls+rs) lx ll (Bin (1+rs+lrs) x lr r)
                     | otherwise -> Bin (1+ls+rs) lrx (Bin (1+lls+size lrl) lx ll lrl) (Bin (1+rs+size lrr) x lrr r)
                   (_, _) -> error "Failure in Data.Map.balanceL"
              | otherwise -> Bin (1+ls+rs) x l r
cool

lmao is this supposed to be human readable code

JewKiller 3000
Nov 28, 2006

by Lowtax
it looks like a translation of the algorithm as written in a textbook (that was hopefully proved correct)

Shaggar
Apr 26, 2006

pointsofdata posted:

code:
balanceL :: a -> Set a -> Set a -> Set a
balanceL x l r = case r of
  Tip -> case l of
           Tip -> Bin 1 x Tip Tip
           (Bin _ _ Tip Tip) -> Bin 2 x l Tip
           (Bin _ lx Tip (Bin _ lrx _ _)) -> Bin 3 lrx (Bin 1 lx Tip Tip) (Bin 1 x Tip Tip)
           (Bin _ lx ll@(Bin _ _ _ _) Tip) -> Bin 3 lx ll (Bin 1 x Tip Tip)
           (Bin ls lx ll@(Bin lls _ _ _) lr@(Bin lrs lrx lrl lrr))
             | lrs < ratio*lls -> Bin (1+ls) lx ll (Bin (1+lrs) x lr Tip)
             | otherwise -> Bin (1+ls) lrx (Bin (1+lls+size lrl) lx ll lrl) (Bin (1+size lrr) x lrr Tip)

  (Bin rs _ _ _) -> case l of
           Tip -> Bin (1+rs) x Tip r

           (Bin ls lx ll lr)
              | ls > delta*rs  -> case (ll, lr) of
                   (Bin lls _ _ _, Bin lrs lrx lrl lrr)
                     | lrs < ratio*lls -> Bin (1+ls+rs) lx ll (Bin (1+rs+lrs) x lr r)
                     | otherwise -> Bin (1+ls+rs) lrx (Bin (1+lls+size lrl) lx ll lrl) (Bin (1+rs+size lrr) x lrr r)
                   (_, _) -> error "Failure in Data.Map.balanceL"
              | otherwise -> Bin (1+ls+rs) x l r
cool

that is unbelievably awful

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
All languages should be designed with syntax highlighting in mind

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison

USSMICHELLEBACHMAN posted:

All languages should be designed with syntax highlighting in mind

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
haskell has unbelievably beautiful syntax when it is properly highlighted and indented

stoutfish
Oct 8, 2012

by zen death robot
i'm the bin

double sulk
Jul 2, 2010

stoutfish posted:

i'm the bin

i'm the lx Tip

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Shaggar posted:

that is unbelievably awful

balancing binary trees in any language sucks rear end since it's all pattern matching

at least pattern matching is natural in Haskell whereas its totally awful in java/C#

code:

 private AvlNode insert( Comparable x, AvlNode t )
        {
            if( t == null )
                t = new AvlNode( x, null, null );
            else if( x.compareTo( t.element ) < 0 )
            {
                t.left = insert( x, t.left );
                if( height( t.left ) - height( t.right ) == 2 )
                    if( x.compareTo( t.left.element ) < 0 )
                        t = rotateWithLeftChild( t );
                    else
                        t = doubleWithLeftChild( t );
            }
            else if( x.compareTo( t.element ) > 0 )
            {
                t.right = insert( x, t.right );
                if( height( t.right ) - height( t.left ) == 2 )
                    if( x.compareTo( t.right.element ) > 0 )
                        t = rotateWithRightChild( t );
                    else
                        t = doubleWithRightChild( t );
            }
            else
                ;  // Duplicate; do nothing
            t.height = max( height( t.left ), height( t.right ) ) + 1;
            return t;
        }


  private static AvlNode rotateWithLeftChild( AvlNode k2 )
        {
            AvlNode k1 = k2.left;
            k2.left = k1.right;
            k1.right = k2;
            k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
            k1.height = max( height( k1.left ), k2.height ) + 1;
            return k1;
        }

        /**
         * Rotate binary tree node with right child.
         * For AVL trees, this is a single rotation for case 4.
         * Update heights, then return new root.
         */
        private static AvlNode rotateWithRightChild( AvlNode k1 )
        {
            AvlNode k2 = k1.right;
            k1.right = k2.left;
            k2.left = k1;
            k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
            k2.height = max( height( k2.right ), k1.height ) + 1;
            return k2;
        }

        /**
         * Double rotate binary tree node: first left child
         * with its right child; then node k3 with new left child.
         * For AVL trees, this is a double rotation for case 2.
         * Update heights, then return new root.
         */
        private static AvlNode doubleWithLeftChild( AvlNode k3 )
        {
            k3.left = rotateWithRightChild( k3.left );
            return rotateWithLeftChild( k3 );
        }

        /**
         * Double rotate binary tree node: first right child
         * with its left child; then node k1 with new right child.
         * For AVL trees, this is a double rotation for case 3.
         * Update heights, then return new root.
         */
        private static AvlNode doubleWithRightChild( AvlNode k1 )
        {
            k1.right = rotateWithLeftChild( k1.right );
            return rotateWithRightChild( k1 );
        }

ick

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

USSMICHELLEBACHMAN posted:

haskell has unbelievably beautiful syntax when it is properly highlighted and indented

I want a screenshot of this

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Symbolic Butt posted:

I want a screenshot of this

just browse through here

http://learnyouahaskell.com/zippers

Workaday Wizard
Oct 23, 2009

by Pragmatica
someone mention network stacks on one of these threads

why is bsd network stack a thing (as in why care that it originated in bsd or whatev) and why does windows having its own network stack matter?

i mean how different can stacks be? it's all just bit shuffling with packets using well defined protocols (tcp/ip etc.)


can someone tell me why should i care?

sry if my quuestion is not clear i have a flu

gonadic io
Feb 16, 2011

>>=

no coloring of constructors :(

this is some code from my game in the Monokai ST3 theme:

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Shinku ABOOKEN posted:

someone mention network stacks on one of these threads

why is bsd network stack a thing (as in why care that it originated in bsd or whatev) and why does windows having its own network stack matter?

i mean how different can stacks be? it's all just bit shuffling with packets using well defined protocols (tcp/ip etc.)


can someone tell me why should i care?

sry if my quuestion is not clear i have a flu

paging hackbunny

MeruFM
Jul 27, 2010

Malcolm XML posted:

balancing binary trees in any language sucks rear end since it's all pattern matching

at least pattern matching is natural in Haskell whereas its totally awful in java/C#

ick

the content size doesn't seem that different.
Java is just bigger bc
1. comments
2. word variables vs character variables


Also what's going on here?
code:
balanceL :: a -> Set a -> Set a -> Set a

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

AlsoD posted:

lens :newlol:

the containers library shows common architecture patterns in nontrivial projects (consideration of strictness, cpp macros, repeated use of "go patterns" instead of naive recursion, even some fusion via RULE pragmas iirc)

anything by released Brian O'Sullivan is probably good
blog: http://www.serpentine.com/blog/
github: https://github.com/bos?tab=repositories
hackage: http://hackage.haskell.org/user/BryanOSullivan

the vector package is a prime example of heavy pointer/ffi use if you want to learn about that specifically

seriously though ekmett does take care to have nice code iirc so you might want to check that out afterwards (even if it does sometimes go off the deep end in terms of abstraction)

cheers! had completely forgotten about o'sullivan and real world haskell, so think ill make a start on that. also lookin through the container lib you linked, hoo boy i got a lot to learn - i remember a lot less from LYAH than i thought i did

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

AlsoD posted:

no coloring of constructors :(

this is some code from my game in the Monokai ST3 theme:


I was expecting something crazier like this ruby thing:

http://chriskempson.github.io/base16/#monokai

it's like they hate white or something

stoutfish
Oct 8, 2012

by zen death robot

Symbolic Butt posted:

I was expecting something crazier like this ruby thing:

http://chriskempson.github.io/base16/#monokai

it's like they hate white or something

i hate whites too

bobbilljim
May 29, 2013

this christmas feels like the very first christmas to me
:shittydog::shittydog::shittydog:

AlsoD posted:

no coloring of constructors :(

this is some code from my game in the Monokai ST3 theme:


this code is much easier to read than the previous examples but i think the main reason for that is that you actually named your variables clearly

BONGHITZ
Jan 1, 1970

maybe

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
haskell is very readable it's just weird if you're coming from an imperative language because it's different

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison

USSMICHELLEBACHMAN posted:

haskell is very readable it's just weird if you're coming from an imperative language because it's different

i keep reading and re-reading that img posted a bit ago and it makes my eyes instantly glaze over in a way that not even C can

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

uncurable mlady posted:

i keep reading and re-reading that img posted a bit ago and it makes my eyes instantly glaze over in a way that not even C can

yeah it's because it's far removed from any language you've learnt

Soldier of Fortran
May 2, 2009

USSMICHELLEBACHMAN posted:

All languages should be designed with syntax highlighting in mind



yeah thats so much better. beautiful.

suffix
Jul 27, 2013

Wheeee!

MeruFM posted:

Also what's going on here?
code:
balanceL :: a -> Set a -> Set a -> Set a

it's a type declaration, so balanceL is a function that takes a thing and two sets of that thing and returns a set of that thing.

or maybe it takes a thing and a set of that thing and returns a function that takes a set of that thing an returns a set of that thing :iiam:

anyway that's just the types, it will all be much clearer when we get to the declaration and see the argument names

code:
balanceL x l r = case r of
oh wait lol nevermind

Valeyard
Mar 30, 2012


Grimey Drawer
Erlang

(sorry Mono)

gonadic io
Feb 16, 2011

>>=

suffix posted:

it's a type declaration, so balanceL is a function that takes a thing and two sets of that thing and returns a set of that thing.

or maybe it takes a thing and a set of that thing and returns a function that takes a set of that thing an returns a set of that thing :iiam:

anyway that's just the types, it will all be much clearer when we get to the declaration and see the argument names

code:
balanceL x l r = case r of
oh wait lol nevermind

there's a comment in the line above that says that it does. it makes a binary node with x as the element, l as the left subtree and r as the right subtree. it's also specialised to check if it needs to rebalance since the left might be bigger than the right (and balanceR is the same for the other case).

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

suffix posted:

it's a type declaration, so balanceL is a function that takes a thing and two sets of that thing and returns a set of that thing.

or maybe it takes a thing and a set of that thing and returns a function that takes a set of that thing an returns a set of that thing :iiam:

anyway that's just the types, it will all be much clearer when we get to the declaration and see the argument names

code:
balanceL x l r = case r of
oh wait lol nevermind

http://groups.csail.mit.edu/mac/users/adams/BB/

it's a left biased balancing of treesets


x is the value of what ur storing, l is the left subtree, r is the right subtree

names are lovely though but seem to be consistent in using x as the value. I assume it's something like data Set a = Tip | Bin Int a (Set a) (Set a) where the int is the height of the tree or something similar

gonadic io
Feb 16, 2011

>>=
Set<TypeOfElement> insertAndPossiblyBalanceLeftSubtree (TypeOfElement element, Set<TypeOfElement> leftSubtree, Set<TypeOfElement> rightSubtree)

Malcolm XML
Aug 8, 2009

I always knew it would end like this.
lol

code:

-- The balance function is equivalent to the following:
--
--   balance :: a -> Set a -> Set a -> Set a
--   balance x l r
--     | sizeL + sizeR <= 1   = Bin sizeX x l r
--     | sizeR > delta*sizeL  = rotateL x l r
--     | sizeL > delta*sizeR  = rotateR x l r
--     | otherwise            = Bin sizeX x l r
--     where
--       sizeL = size l
--       sizeR = size r
--       sizeX = sizeL + sizeR + 1
--
--   rotateL :: a -> Set a -> Set a -> Set a
--   rotateL x l r@(Bin _ _ ly ry) | size ly < ratio*size ry = singleL x l r
--                                 | otherwise               = doubleL x l r
--   rotateR :: a -> Set a -> Set a -> Set a
--   rotateR x l@(Bin _ _ ly ry) r | size ry < ratio*size ly = singleR x l r
--                                 | otherwise               = doubleR x l r
--
--   singleL, singleR :: a -> Set a -> Set a -> Set a
--   singleL x1 t1 (Bin _ x2 t2 t3)  = bin x2 (bin x1 t1 t2) t3
--   singleR x1 (Bin _ x2 t1 t2) t3  = bin x2 t1 (bin x1 t2 t3)
--
--   doubleL, doubleR :: a -> Set a -> Set a -> Set a
--   doubleL x1 t1 (Bin _ x2 (Bin _ x3 t2 t3) t4) = bin x3 (bin x1 t1 t2) (bin x2 t3 t4)
--   doubleR x1 (Bin _ x2 t1 (Bin _ x3 t2 t3)) t4 = bin x3 (bin x2 t1 t2) (bin x1 t3 t4)
--
-- It is only written in such a way that every node is pattern-matched only once.
--
-- Only balanceL and balanceR are needed at the moment, so balance is not here anymore.
-- In case it is needed, it can be found in Data.Map.

-- Functions balanceL and balanceR are specialised versions of balance.
-- balanceL only checks whether the left subtree is too big,
-- balanceR only checks whether the right subtree is too big.

-- balanceL is called when left subtree might have been inserted to or when
-- right subtree might have been deleted from.
its just a fused version of that so there u go, u can totally write confusing haskell if u want to and Base has to do it since they want to compile on every stupid haskell compiler ever

gonadic io
Feb 16, 2011

>>=

Malcolm XML posted:

lol

its just a fused version of that so there u go, u can totally write confusing haskell if u want to and Base has to do it since they want to compile on every stupid haskell compiler ever

even worse is that they can't use any convenience libraries

distortion park
Apr 25, 2011


that reminds me is there a reason you have to import something to get foldl' instead of just foldl (also i thought that the ' is meant to denote side effects or something?)

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

pointsofdata posted:

that reminds me is there a reason you have to import something to get foldl' instead of just foldl (also i thought that the ' is meant to denote side effects or something?)

the ' (called "prime") can denote a couple of things, but in this case it's strictness. foldl' evaluates its initial param before recursing

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

pointsofdata posted:

that reminds me is there a reason you have to import something to get foldl' instead of just foldl (also i thought that the ' is meant to denote side effects or something?)

backwards compat is why the prelude sucks and is lazy list heavy


foldl' won't cause memory leaks with huge finite lists since it is strict in the accumulator

gonadic io
Feb 16, 2011

>>=

pointsofdata posted:

that reminds me is there a reason you have to import something to get foldl' instead of just foldl (also i thought that the ' is meant to denote side effects or something?)

historical reasons :(

the ' means that it's strict.

it's late for me so I'm just going to link this discussion of the differences than type one up myself but to be brief:

there's no reason to ever use foldl instead of foldl' since the way it works is that you need to evaluate stuff to do anything anyway so you might as well be strict which speeds things up and makes it run in constant space

foldr on the other hand you DON'T want to be strict (and hence there's no foldr') since the way it's set up is that if you have a function that is productive* then you can lazily evaluate its results

*: by "productive" here I mean that it will add an actual data constructor in front of your thing i.e. adding elements on front of a list or whatever

tl;dr: if you have things that don't benefit from laziness (ints for example) use foldl' and if you have things that you do want to be lazy (lists for example) use foldr.

double sulk
Jul 2, 2010

haskell is a useless language deemed as such by its lead designer and exists solely for the purpose of improving more useful languages like c#

gonadic io
Feb 16, 2011

>>=
common haskell naming conventions:
a ' after a haskell function means it's a strict version
a M afterwards means it's a side-effecting (monadic) version and returns the results too
a M_ afterwards means that it does side effects but throws away the results

see: map, map', mapM, mapM_, mapM'_

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

double sulk posted:

haskell is a useless language deemed as such by its lead designer and exists solely for the purpose of improving more useful languages like c#

sulk have you done anything of note in haskell or c# or did you just watch that four minute video of spj and Form An Opinion

Adbot
ADBOT LOVES YOU

gonadic io
Feb 16, 2011

>>=

double sulk posted:

haskell is a useless language deemed as such by its lead designer and exists solely for the purpose of improving more useful languages like c#

people (you and shaggar) keep citing this triumphantly, have you watched the rest of the video (from 3-4 years ago) where he gives a little more context on that statement?

  • Locked thread