Live data from Hacker News

Functional Programming Patterns

slideshare.net

61–70 of 70 posts

Re: Functional Programming Patterns

#61
post #60
post #59

Earlier quoted context omitted.

See my link down thread. I think this really comes down to the murkiness in definitions of declarative and imperative. I did find a later definition that points out that these are no longer duals of each other. I was actively avoiding the obviously imperative functions, though. Not sure why. That is, I could have simply said that the existence of all of the functions that end in exclamation points shows the language…

The imperativeness of Racket really doesn't come so much from the *! functions, but instead things like `begin` which explicitly sequence program statements in such a way that only allows them to interact via external mutable state.

I have a feeling we are on yet different definitions of imperative and such.

    (define x 3)
    (display x)
    (set! x (add1 x))
    (display x)
is about as imperative as they come, by my definition. And not a begin in site.

Re: Functional Programming Patterns

#62
post #34
post #24

Earlier quoted context omitted.

16 of 23 patterns have qualitatively simpler implementation in Lisp or Dylan than in C++ for at least some uses of each pattern Things like this are literally why I switched from Python to Racket almost immediately once I discovered the latter. So much time spent fighting an imperative language to do something that seemed like it should just be easier in the first place.

Racket is an imperative language, as well. Right? Don't get me wrong, I love Racket. Just I don't think of it as !imperative. I am also unconvinced that the imperative nature of many languages is the problem. Not sure what it is.

Goodness. My apologies for instigating a terminology war, I should not have been so imprecise.

I think tel is mostly on the right track of what I mean when I compare Racket to traditional ALGOL languages, but it is also true to say that Racket is not strictly non-imperative either: it is ultimately a multi-paradigm language, just one that culturally leans harder towards a functional-declarative style. My early projects were just as lousy with mutable state and in-order processing as anything I wrote in Python; but I got better.

I think what really drove me to Lisp and FP languages was the notion of nearly everything being first-class values, from functions to objects, and it's this quality, compared to the tedium of building yet another !"¤&¤&! constructor pattern that really attracted me. This, plus things like macros, the way the language allows for function composition so readily, is what made Lisp feel like 'how programming should've been all along' for me. Haskell as well induced that same reaction in other ways (mmm ... curry ...), though I'm still not entirely convinced that such strict purity is practical.

Re: Functional Programming Patterns

#63

Excellent presentation. Of the leading type safe functional languages (Haskell, F#, and OCaml) I find F# to be far and away the most accessible in terms of syntax and application. Writing Scala in my day job currently (which, for the most part, I quite enjoy) but can see jumping ship if Microsoft's move to Linux is successful. Being able to develop and deploy F# applications on Linux with full blown Type Providers an…

Do you know if it is possible to use F# on windows without having to install several GB of crap? I'd like to be able to try it out, but the same way I would try any other language, install the compiler and try it out. All I can find is huge packages including visual studio and all kinds of other junk.

There is an online editor/REPL/tutorial at http://www.tryfsharp.org/

You will need the Silverlight plugin.

Re: Functional Programming Patterns

#64
post #61
post #60

Earlier quoted context omitted.

The imperativeness of Racket really doesn't come so much from the *! functions, but instead things like `begin` which explicitly sequence program statements in such a way that only allows them to interact via external mutable state.

I have a feeling we are on yet different definitions of imperative and such. (define x 3) (display x) (set! x (add1 x)) (display x) is about as imperative as they come, by my definition. And not a begin in site.

Yeah, there's sort of an implicit begin around the whole thing, though.

Re: Functional Programming Patterns

#65
post #62
post #34

Earlier quoted context omitted.

Racket is an imperative language, as well. Right? Don't get me wrong, I love Racket. Just I don't think of it as !imperative. I am also unconvinced that the imperative nature of many languages is the problem. Not sure what it is.

Goodness. My apologies for instigating a terminology war, I should not have been so imprecise. I think tel is mostly on the right track of what I mean when I compare Racket to traditional ALGOL languages, but it is also true to say that Racket is not strictly non-imperative either: it is ultimately a multi-paradigm language, just one that culturally leans harder towards a functional-declarative style. My early projec…

If my subthread sounded at all like an attack against Lisp and Racket, than I humbly apologize. So far, I am loving the language and what can do with it.

And I like currying and such, as well. Even use it in Racket quote often. Isn't too uncommon to see something like in my projects.

    ((compose
        (curry func1 val1)
        func2
        func3) seed)
I think what really makes Racket so amazing to me is just how easy it is to iteratively write a function. Start with a core, and gradually append around it more and more till it is done. Testing with values throughout the whole process.

I also like the literature around Lisp. Land of Lisp and SICP are both very well written and just plain fun to read.

Re: Functional Programming Patterns

#66
post #65
post #62

Earlier quoted context omitted.

Goodness. My apologies for instigating a terminology war, I should not have been so imprecise. I think tel is mostly on the right track of what I mean when I compare Racket to traditional ALGOL languages, but it is also true to say that Racket is not strictly non-imperative either: it is ultimately a multi-paradigm language, just one that culturally leans harder towards a functional-declarative style. My early projec…

If my subthread sounded at all like an attack against Lisp and Racket, than I humbly apologize. So far, I am loving the language and what can do with it. And I like currying and such, as well. Even use it in Racket quote often. Isn't too uncommon to see something like in my projects. ((compose (curry func1 val1) func2 func3) seed) I think what really makes Racket so amazing to me is just how easy it is to iteratively…

Yes. I find sometimes that programming in Racket and other Lisps is so efficient, that whenever something starts looking tedious, I start reconsidering if there's a more elegant way I'm missing to implement it. Learning it, hacking it, and reading about it (and Scheme and other Lisps as well as Haskell) have done marvelous things for my programming skills.

Today I wrote a struct system in 29 lines of code. Kinda hard sometimes to do that and then even think about going back to, well, much of anything but another Lisp or composable FP language, really.

Re: Functional Programming Patterns

#67

I am currently taking EdX's Intro to Functional Programming mooc, taught by Erik Meijer (with his crazy shirts), which uses Haskell to teach functional programming concepts. I am wondering if there are any other good resources for teaching the functional programming paradigms. Anybody care to recommend me some resources? Also, I mainly work with Ruby and Javascript in my full time job. Currently, in school, I use Jav…

This reply is a little late, but there is a series on the (non-free-but-worth-it-for-one month) "Frontend Masters" site. It is called Hardcore Functional Programming (or similar) and goes into Monads, Functors in normal JavaScript.

Re: Functional Programming Patterns

#68

I Really found the book "Learn you a haskell" to be an really fast way to get functional programming into production. I did not however continue to use haskell since it doesnt have key libraries I need. I instead adopted livescript,underscore into javascript and was good to go.

Out of curiosity, what libraries did you need?

I wanted to use a library to do simple linear algebra, quick and dirty regular expression.

With linear algebra the library HMatrix is broken in more than one place, it also requires me to learn a lot of new types just to do simple matrix decomposition.

Regular expression in haskell requires me to learn a completely new way to do them, I am not under the impression that regular expression is broken in any way in python or javascript that it requires new syntax.

Haskell also does not have access to the browser runtime which is key if you are want maximum exposure for your work - ( no one is interested in binaries anymore ).

There is purescript but its a large layer over javascript and even a simple hello world results in a 2000 LOC compilation of javascript.

I am not a computer programmer, I am an applied Mathematician and haskell requires a lot of work before it can be used for serious work the type of work I do.

Haskell doesnt have the use of use that is offered by things like npm, browserify which are the most awesome tools I have used.

Re: Functional Programming Patterns

#69

Earlier quoted context omitted.

Out of curiosity, what libraries did you need?

I wanted to use a library to do simple linear algebra, quick and dirty regular expression. With linear algebra the library HMatrix is broken in more than one place, it also requires me to learn a lot of new types just to do simple matrix decomposition. Regular expression in haskell requires me to learn a completely new way to do them, I am not under the impression that regular expression is broken in any way in pytho…

> There is purescript but its a large layer over javascript and even a simple hello world results in a 2000 LOC compilation of javascript.

This really depends on which compilation route you take. `psc-make` will include all of the Prelude and compile to CommonJS modules, which can result in quite a bit of code, but `psc` will trim out all unused functions, and should result in about 10 LOC for Hello World.

Re: Functional Programming Patterns

#70

Earlier quoted context omitted.

Out of curiosity, what libraries did you need?

I wanted to use a library to do simple linear algebra, quick and dirty regular expression. With linear algebra the library HMatrix is broken in more than one place, it also requires me to learn a lot of new types just to do simple matrix decomposition. Regular expression in haskell requires me to learn a completely new way to do them, I am not under the impression that regular expression is broken in any way in pytho…

> With linear algebra the library HMatrix is broken in more than one place, it also requires me to learn a lot of new types just to do simple matrix decomposition.

What was broken about HMatrix? What library has the best API to you? Algebra[0] from Node?

    var algebra = require('algebra');
    
    var M = algebra.MatrixSpace;
    var R = algebra.Real;
    
    // Create the space of 2x2 real matrices
    var R2x2 = new M(R, 2);
    
    // Create two invertible matrices:
    //
    //       | 1 2 |         | -1 0 |
    //       | 3 4 |   and   |  0 1 |
    //
    var m1 = new R2x2.Matrix([1, 2,
                              3, 4]);
    
    var m2 = new R2x2.Matrix([-1, 0,
                               0, 1]);
    
    // Multiply m1 by m2 at right side
    //       | 1 2 | * | -1 0 | = | -1 2 |
    //       | 3 4 |   |  0 1 |   | -3 4 |
    m1.mul(m2);
    
    console.log(m1.data); // [-1, 2, -3, 4]
    
    // Check out m1 determinant, should be 2 = (-1) * 4 - (-3) * 2
    console.log(m1.determinant.data); // 2
> Regular expression in haskell requires me to learn a completely new way to do them, I am not under the impression that regular expression is broken in any way in python or javascript that it requires new syntax.

What new syntax? I'm not sure what you mean by "learn a completely new way to do them".

Haskell (pcre-light[1]):

    > import Text.Regex.PCRE.Light
    > let numberRgx = compile "[0-9]+" []
    > match numberRgx "the answer is 42" []
    Just ["42"]
Node:

/[0-9]+/g.exec("the answer is 42")[0];

Haskell (regex-tdfa[2]):

> import Text.Regex.TDFA > "the answer is 42" =~ "[0-9]+" :: String "42"

Ahh, I think I see what you mean now. You want the familiar /regex/string syntax right? There used to be a QuasiQuoter (template haskell library) that allowed syntax like:

    ghci> [$rx|[0-9]+|] "the answer is 42"
    Just ["42"]
However it[3] seems to have bitrotted and everyone else is using the "needle" =~ "match" syntax.

EDIT: Found a useful website comparing language implementations, namely regex in perl vs Haskell:

http://langref.org/haskell+perl/pattern-matching

0: http://g14n.info/algebra/examples/quick-start

1: http://hackage.haskell.org/package/pcre-light

2: http://hackage.haskell.org/package/regex-tdfa

3: http://hackage.haskell.org/package/regexqq (bitrotted)

Post reply on HN