Functional Programming Book, questions.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Functional Programming Book, questions.

jlm@justinfront.net
Hi have just bought a book on functional programming (Real World  
Functional Programming ISBN 13: 978-1-933988-92-4 ) so I can  
understand more of the technical discussions on the mailing list, it  
covers F# ( there were none on Ocaml in the shop ), I must admit I had  
not realized that F# would be quite so tricky to use on a mac, and I  
am yet to compile directly from textmate, but managed to compile from  
the command line an OpenTK example into an app... but even if  
compiling is not easy... I suspect the learning will be the really  
tricky part.. if I get so far.

Anyway I started this thread to ask some questions about functional  
programming in haXe, really to get a better understanding of how  
functionally it can be used, probably in comparison with F#.  Its not  
so easy to follow all the concepts so my questions at the moment are  
probably rather basic and lack understanding.

(Ref to chapter 2)
1) Does haXe have anything like the pipelining operator   |>   so a  
series of operations can be applied without nesting function calls.
2) Is a discriminated union like a typedef?
3) Can you use Switch to do 'match' type pattern matching and nested  
patterns?
4) haxe does not have units of measure that you might apply to a  
float....   50.0<kmperh> != 40.0<mileperh> , is that because they are  
hard to implement or just not that useful?

General question
5) Could the haXe compiler be compiled with fsc.exe with minor changes?

If these questions are not so clear maybe I can clarify by posting a  
code snipit from the book, but always a bit cautious about posting  
book examples.


Cheers
;j



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Functional Programming Book, questions.

Justin Donaldson-2
Here's how I understand how haxe currently handles these...

On Mon, Aug 30, 2010 at 6:04 AM, [hidden email] <[hidden email]> wrote:
1) Does haXe have anything like the pipelining operator   |>   so a series of operations can be applied without nesting function calls.

Nope, although you can approximate the effect with "using":

using Lambda;
using Math;
[...]
[1,2,3,4].map(4.pow).filter(function(x) return x > 16;)

 
2) Is a discriminated union like a typedef?

This sounds more like an upcoming haxe feature called a multitype:
http://haxe.org/com/features
 
3) Can you use Switch to do 'match' type pattern matching and nested patterns?

Switch has special syntax for matching the special haxe enums, but operates normally otherwise AFAIK.
 
4) haxe does not have units of measure that you might apply to a float....   50.0<kmperh> != 40.0<mileperh> , is that because they are hard to implement or just not that useful?

No, but it might be useful.  Once again, "using" might work...

trace(50.mph() > 60.kph());

mph() and kph() would both convert their arguments to some universal unit, such as units of radius of the earth ~(6371 km / 3958 miles) per second.
 
Frink does this sort of thing really well:
http://futureboy.us/frinkdocs/


General question
5) Could the haXe compiler be compiled with fsc.exe with minor changes?


???
 
If these questions are not so clear maybe I can clarify by posting a code snipit from the book, but always a bit cautious about posting book examples.


Cheers
;j



--
haXe - an open source web programming language
http://haxe.org



--
blog: http://www.scwn.net
aim: iujjd
twitter: jjdonald


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Functional Programming Book, questions.

MarcWeber
In reply to this post by jlm@justinfront.net
Hi jln,

"functional" can be interpreted in varyous ways. I learned Haskell in the
past (the haskell wiki has many nice tutorials and stuff you can read
without paying for it).

In Haskell functional means:

- same input always yields same output (no side effects such as global
  vars)
  This means: Never do things in place, all operations changing
  something returns a fresh copy.
  In Haskell you can even "ensure" by the type system that a function
  has no side effects (unless you use unsafePerformIO which is
  considered evil)
  Some of the censqeunces are: The compiler has more choices how often
  to run which code - but the result must be the same.

- usually higher order functions such as map, fold and more abstractions
  (up to monads, Applicative etc)..

- probably it means more. But that's one of the key points as I
  understand it.

In Haskell you write g . f  which is the same as g(f(input))
Probably this corresponds to the pipelining in F#.

> (Ref to chapter 2)
> 1) Does haXe have anything like the pipelining operator   |>   so a  
> series of operations can be applied without nesting function calls.
Not yet. You have to use g(f(..))

> 3) Can you use Switch to do 'match' type pattern matching and nested  
> patterns?
Depends, can you match on values? I never tried. You can match enums.

> 4) haxe does not have units of measure that you might apply to a  
> float....   50.0<kmperh> != 40.0<mileperh> , is that because they are  
> hard to implement or just not that useful?

This is an implementation detail. It has notheing to do with
"functional" in general. You can model it as in any other OOP like
language:

class KMPERH {
  var value
  public function add(..){
  }
  public function sub(..){
  }
}

or such to ensure that you only operate on matching types.
But the HaXe type system is not as strong as the Haskell one.
(Eg http://haskell.org/haskellwiki/Physical_units)

So in HaXe you can't have both: speed and type safety for this use case
AFAIK.

If you want to learn the "functional" thing HaXe may not be the best
choice. Haskell, ocaml, F# etc maybe a a better fit.
Haskell is special: Its functional and lazy (by default)

Some topics like transactional memory are a perfect fit for Haskell -
however it depends on your application. Sometimes a well known database
is the faster way to get a job done..

When it comes to real world application HaXe can be a good fit for many
use cases - because the language is still simple and it can run on many
targets. Eg there is a Haskell to JS compiler. However I think that
performance drops much more than using HaXe (I haven't tried this yet,
though)

Marc Weber

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Functional Programming Book, questions.

Lee Sylvester
In reply to this post by jlm@justinfront.net
Just as an aside; Neko has a functional language, called NekoML. The
Neko compiler is bootstrapped with this language. It's a great language
to learn and can be used alongside haXe, if you like. It's a little
nicer than OCaml (that I've seen).

Lee




[hidden email] wrote:

> Hi have just bought a book on functional programming (Real World
> Functional Programming ISBN 13: 978-1-933988-92-4 ) so I can
> understand more of the technical discussions on the mailing list, it
> covers F# ( there were none on Ocaml in the shop ), I must admit I had
> not realized that F# would be quite so tricky to use on a mac, and I
> am yet to compile directly from textmate, but managed to compile from
> the command line an OpenTK example into an app... but even if
> compiling is not easy... I suspect the learning will be the really
> tricky part.. if I get so far.
>
> Anyway I started this thread to ask some questions about functional
> programming in haXe, really to get a better understanding of how
> functionally it can be used, probably in comparison with F#.  Its not
> so easy to follow all the concepts so my questions at the moment are
> probably rather basic and lack understanding.
>
> (Ref to chapter 2)
> 1) Does haXe have anything like the pipelining operator   |>   so a
> series of operations can be applied without nesting function calls.
> 2) Is a discriminated union like a typedef?
> 3) Can you use Switch to do 'match' type pattern matching and nested
> patterns?
> 4) haxe does not have units of measure that you might apply to a
> float....   50.0<kmperh> != 40.0<mileperh> , is that because they are
> hard to implement or just not that useful?
>
> General question
> 5) Could the haXe compiler be compiled with fsc.exe with minor changes?
>
> If these questions are not so clear maybe I can clarify by posting a
> code snipit from the book, but always a bit cautious about posting
> book examples.
>
>
> Cheers
> ;j
>
>
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Functional Programming Book, questions.

blackdog-2

nekoml not so easy to learn as it's not documented, last time I looked.

two good resources for ocaml

http://ocamlunix.forge.ocamlcore.org/
http://caml.inria.fr/pub/docs/oreilly-book/html/index.html

bd

On 08/30/2010 11:38 AM, Lee McColl Sylvester wrote:
Just as an aside; Neko has a functional language, called NekoML. The Neko compiler is bootstrapped with this language. It's a great language to learn and can be used alongside haXe, if you like. It's a little nicer than OCaml (that I've seen).

Lee




[hidden email] wrote:
Hi have just bought a book on functional programming (Real World Functional Programming ISBN 13: 978-1-933988-92-4 ) so I can understand more of the technical discussions on the mailing list, it covers F# ( there were none on Ocaml in the shop ), I must admit I had not realized that F# would be quite so tricky to use on a mac, and I am yet to compile directly from textmate, but managed to compile from the command line an OpenTK example into an app... but even if compiling is not easy... I suspect the learning will be the really tricky part.. if I get so far.

Anyway I started this thread to ask some questions about functional programming in haXe, really to get a better understanding of how functionally it can be used, probably in comparison with F#.  Its not so easy to follow all the concepts so my questions at the moment are probably rather basic and lack understanding.

(Ref to chapter 2)
1) Does haXe have anything like the pipelining operator   |>   so a series of operations can be applied without nesting function calls.
2) Is a discriminated union like a typedef?
3) Can you use Switch to do 'match' type pattern matching and nested patterns?
4) haxe does not have units of measure that you might apply to a float....   50.0<kmperh> != 40.0<mileperh> , is that because they are hard to implement or just not that useful?

General question
5) Could the haXe compiler be compiled with fsc.exe with minor changes?

If these questions are not so clear maybe I can clarify by posting a code snipit from the book, but always a bit cautious about posting book examples.


Cheers
;j







--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Functional Programming Book, questions.

jlm@justinfront.net
In reply to this post by Justin Donaldson-2
 
4) haxe does not have units of measure that you might apply to a float....   50.0<kmperh> != 40.0<mileperh> , is that because they are hard to implement or just not that useful?

No, but it might be useful.  Once again, "using" might work...

trace(50.mph() > 60.kph());

mph() and kph() would both convert their arguments to some universal unit, such as units of radius of the earth ~(6371 km / 3958 miles) per second.
 
Frink does this sort of thing really well:
http://futureboy.us/frinkdocs/


The 'units of measure' example had some conversion code, if haXe had units of measure it might look like...

var mphTokph = function( speed:Float<'mph'> ){ return speed*1.6<'kph'>; };

I think it would be better to use 'using' with the conversion than on the typing...

using mphTokph;

var ukMySpeed = 60<'mph'>;

if( ukMySpeed.mphTokph() > frenchLimit ) gotoJail();

... using is just so easy to abuse, I would like to sometimes be able to localize it to a method, since it can do some crazy stuff that will confuse easily, ofcourse ideally you would define a conversion, I am not sure what that might look like... I guess sometimes its not just a one to one, but ideally you could just define the mapping one way and infer the other

Anyway I did not mean to get into the asking for new stuff in HaXe as really this is not a must have feature, but it's good to learn a bit about other languages (code or otherwise).



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Functional Programming Book, questions.

MarcWeber
> var mphTokph = function( speed:Float<'mph'> ){ return  
> speed*1.6<'kph'>; };

That's not enough. You also want

var x:Float<'kph'> = 10;
var miles:Float<'miles'> = 20;

var y = x * x; // to derive type Float<'kph'*'kph'>

and disallow
trace ( x + miles );

If HaXe gets a HaXe interpreter itself (for macro like programming) this
will be easy to implement.

I don't have a use case for this kind of deriving in HaXe at the moment.

So for me its a maybe future vision only.

Marc Weber

--
haXe - an open source web programming language
http://haxe.org