Re: Introduction to Functional Programming in OCaml at Paris Diderot (MOOC)

"Sergei Steshenko [email protected] [ocaml_beginners]" <[email protected]> Thu, 18 Aug 2016 02:45:51 +0000 (UTC)
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
"If an array has 100 elements in it and I want to access the element at index #83, how is that random access?" - it is random in the sense you single out one element.
Regardless of language an array is supposed to be used for an operation on all the elements. For example, you add two arrays element by element - like you add two audio streams on per buffer of each stream basis. Or you multiply all elements of array by the same value - like you have SW volume control for an audio buffer.

If element #83 needs to be accessed while other do not need to be accessed, it looks like the element is more of a single variable.

There are less straightforward cases - like you conduct fast Fourier transform - on one hand, you do it on per buffer basis, on the other hand, access to the array elements is not that sequential and it depends on FFT algorith being used.

--Sergei.

 
      From: "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]>
 To: [email protected] 
 Sent: Thursday, August 18, 2016 4:47 AM
 Subject: Re: "ocaml_beginners"::[] Introduction to Functional Programming in OCaml at Paris Diderot (MOOC)
   
    Hendrik,
Thanks for the reply.  Yes, I like that Ocaml is functional but not exclusively functional.  It's a nice language.  I just wish there were more books out there to support the language.  There are hundreds of books on C, C++, Java, Python, Ruby, etc but very little good documentation to support Ocaml.  ( There's some books out there.  I like the stuff by Whitington.  Real World Ocaml is okay, but some of it is just way over my head at this point in my development as a programmer. )
Regarding arrays, here's a question.  What do you mean by "random access"?  If an array has 100 elements in it and I want to access the element at index #83, how is that random access?  What's random about it?  As for inserting elements into the middle of an array, yes, I can see how that would be very problematic.  I remember trying that in Java once.  It was a fun exercise to do, but does require some careful planning if you want the program to work properly.  
Thanks for the feedback.  Have a great week!
Best,
Doug.

On Wed, Aug 17, 2016 at 6:17 PM, Hendrik Boom [email protected] [ocaml_beginners] <[email protected]> wrote:

     On Wed, Aug 17, 2016 at 04:10:43PM -0500, Douglas Lewit [email protected] [ocaml_beginners] wrote:
> I'm replying here to an older message, but what the hell. Anyhow, the
> course looks great. I wish it started sooner than September 26th. By
> "Informatics" I'm assuming they mean "analysis of data" or the combination
> of statistics and computer programming. Or is there another definition for
> Informatics?
> 
> I'm kind of proud of the following functions that I created while playing
> around with the language. I was wondering how I could reverse a list
> without resorting to the @ operator. Well I think I found a way to do it.
> 
> So.... ( Copied from memory, so forgive typos, mistakes, etc. )
> 
> *exception Empty_List of string ;;*
> 
> *(* Here I am borrowing the names of pre-defined functions in Haskell. *)*
> 
> *let rec last = function *
> * |[ ] -> raise ( Empty_List "An empty list does not
> have a last element!" )*
> * |head :: tail -> if tail = [ ] then head else last
> tail ;;*
> 
> *let rec init = function*
> * |[ ] -> raise ( Empty_List "The init function only
> applies to non-empty lists." )*
> * |head :: tail -> if tail <> [ ] then head :: init
> tail else [ ] ;;*
> 
> *let rec reverse = function *
> * |[ ] -> [ ]*
> * |lst -> ( last lst ) :: reverse ( init lst )
> ;;*
> 
> *I'm pretty happy with these functions, especially since the cons operator
> or :: is supposed to be more efficient or faster than the append operator
> or @.*
> 
> Of course I have to wonder why recursing over lists is better than
> iterating over arrays. In traditional imperative programming all these
> things are done through iteration and array manipulation. But in Ocaml and
> Haskell these things are done by recursing over lists or linked lists
> rather than arrays. ( Although I realize that Ocaml does offer the array
> data structure. I'm not really sure if Haskell has arrays. I don't know
> that much about Haskell. ) So then this begs the questions: 1) Why is
> recursion superior to iteration?, and 2) Why are lists better than arrays?
> I know I'm playing the Devil's Advocate here, but it might be good to
> reflect on why functional programming is the way to go rather than studying
> a more "traditional" language such as C or C++ or Java.

The traditional languages force you to pay too much attention on the 
details of data representation. What's more, C and C++ are not 
type-safe. It is very easy to overwrite random pieces of storage with 
junk and not experience the consequences for a very long time, making 
debugging difficult.

> I also recently
> read some blog post where the author said something like, "We need mutable
> data when modeling phenomena in the real world!" He was casting some doubt
> on the legitimacy of the practice of allowing only immutable data types in
> a programming language. He basically said that you have to assign,
> reassign and update variables in order to successfully model real world
> data. Is that really true?

It is possible to do this functionally. But it my not be a convenient 
way to model such phenomena. At some point you are likely to want to 
perform actions on the world, and that is an effect on the world.

> Any thoughts? I appreciate the feedback.
> Have a wonderful day.

I've found the notation and semantics of functional programming to be 
wonderfully compact and clear for most purposes.

But I'd never be happy with a language that did not have imperative 
features. It would be like programming with one hand tied behind my 
back. Figuring out how to get the effect of simple imperative actions 
in a language that has none is a major research project.
 
Which is why I use OCaml, and not Haskell.

As for arrays, they are efficient when you have a fixed-size bunch of 
values of uniform type, and you don't want to do things like 
inserting one in the middle and shoving the others over, adding new 
elements anywhere, and the like, but you do want to index into tham 
more or less at random (Let me see element 83 now.).

Lists are better for for these dynamic patterns of usage if you are 
*not* interested in indexing into them at random.

By the way, the ancient programming language APL had a lot of 
operations that accomplished functional manipulation of arrays.
But it wasn't a functional language by any means. Its array handling 
was an interesting and elegant functional outpost in an otherwise ugly 
procedural language.

-- hendrik
   

  #yiv1858260842 #yiv1858260842 -- #yiv1858260842ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv1858260842 #yiv1858260842ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv1858260842 #yiv1858260842ygrp-mkp #yiv1858260842hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv1858260842 #yiv1858260842ygrp-mkp #yiv1858260842ads {margin-bottom:10px;}#yiv1858260842 #yiv1858260842ygrp-mkp .yiv1858260842ad {padding:0 0;}#yiv1858260842 #yiv1858260842ygrp-mkp .yiv1858260842ad p {margin:0;}#yiv1858260842 #yiv1858260842ygrp-mkp .yiv1858260842ad a {color:#0000ff;text-decoration:none;}#yiv1858260842 #yiv1858260842ygrp-sponsor #yiv1858260842ygrp-lc {font-family:Arial;}#yiv1858260842 #yiv1858260842ygrp-sponsor #yiv1858260842ygrp-lc #yiv1858260842hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv1858260842 #yiv1858260842ygrp-sponsor #yiv1858260842ygrp-lc .yiv1858260842ad {margin-bottom:10px;padding:0 0;}#yiv1858260842 #yiv1858260842actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv1858260842 #yiv1858260842activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv1858260842 #yiv1858260842activity span {font-weight:700;}#yiv1858260842 #yiv1858260842activity span:first-child {text-transform:uppercase;}#yiv1858260842 #yiv1858260842activity span a {color:#5085b6;text-decoration:none;}#yiv1858260842 #yiv1858260842activity span span {color:#ff7900;}#yiv1858260842 #yiv1858260842activity span .yiv1858260842underline {text-decoration:underline;}#yiv1858260842 .yiv1858260842attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv1858260842 .yiv1858260842attach div a {text-decoration:none;}#yiv1858260842 .yiv1858260842attach img {border:none;padding-right:5px;}#yiv1858260842 .yiv1858260842attach label {display:block;margin-bottom:5px;}#yiv1858260842 .yiv1858260842attach label a {text-decoration:none;}#yiv1858260842 blockquote {margin:0 0 0 4px;}#yiv1858260842 .yiv1858260842bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv1858260842 .yiv1858260842bold a {text-decoration:none;}#yiv1858260842 dd.yiv1858260842last p a {font-family:Verdana;font-weight:700;}#yiv1858260842 dd.yiv1858260842last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv1858260842 dd.yiv1858260842last p span.yiv1858260842yshortcuts {margin-right:0;}#yiv1858260842 div.yiv1858260842attach-table div div a {text-decoration:none;}#yiv1858260842 div.yiv1858260842attach-table {width:400px;}#yiv1858260842 div.yiv1858260842file-title a, #yiv1858260842 div.yiv1858260842file-title a:active, #yiv1858260842 div.yiv1858260842file-title a:hover, #yiv1858260842 div.yiv1858260842file-title a:visited {text-decoration:none;}#yiv1858260842 div.yiv1858260842photo-title a, #yiv1858260842 div.yiv1858260842photo-title a:active, #yiv1858260842 div.yiv1858260842photo-title a:hover, #yiv1858260842 div.yiv1858260842photo-title a:visited {text-decoration:none;}#yiv1858260842 div#yiv1858260842ygrp-mlmsg #yiv1858260842ygrp-msg p a span.yiv1858260842yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv1858260842 .yiv1858260842green {color:#628c2a;}#yiv1858260842 .yiv1858260842MsoNormal {margin:0 0 0 0;}#yiv1858260842 o {font-size:0;}#yiv1858260842 #yiv1858260842photos div {float:left;width:72px;}#yiv1858260842 #yiv1858260842photos div div {border:1px solid #666666;min-height:62px;overflow:hidden;width:62px;}#yiv1858260842 #yiv1858260842photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv1858260842 #yiv1858260842reco-category {font-size:77%;}#yiv1858260842 #yiv1858260842reco-desc {font-size:77%;}#yiv1858260842 .yiv1858260842replbq {margin:4px;}#yiv1858260842 #yiv1858260842ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv1858260842 #yiv1858260842ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv1858260842 #yiv1858260842ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv1858260842 #yiv1858260842ygrp-mlmsg select, #yiv1858260842 input, #yiv1858260842 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv1858260842 #yiv1858260842ygrp-mlmsg pre, #yiv1858260842 code {font:115% monospace;}#yiv1858260842 #yiv1858260842ygrp-mlmsg * {line-height:1.22em;}#yiv1858260842 #yiv1858260842ygrp-mlmsg #yiv1858260842logo {padding-bottom:10px;}#yiv1858260842 #yiv1858260842ygrp-msg p a {font-family:Verdana;}#yiv1858260842 #yiv1858260842ygrp-msg p#yiv1858260842attach-count span {color:#1E66AE;font-weight:700;}#yiv1858260842 #yiv1858260842ygrp-reco #yiv1858260842reco-head {color:#ff7900;font-weight:700;}#yiv1858260842 #yiv1858260842ygrp-reco {margin-bottom:20px;padding:0px;}#yiv1858260842 #yiv1858260842ygrp-sponsor #yiv1858260842ov li a {font-size:130%;text-decoration:none;}#yiv1858260842 #yiv1858260842ygrp-sponsor #yiv1858260842ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv1858260842 #yiv1858260842ygrp-sponsor #yiv1858260842ov ul {margin:0;padding:0 0 0 8px;}#yiv1858260842 #yiv1858260842ygrp-text {font-family:Georgia;}#yiv1858260842 #yiv1858260842ygrp-text p {margin:0 0 1em 0;}#yiv1858260842 #yiv1858260842ygrp-text tt {font-size:120%;}#yiv1858260842 #yiv1858260842ygrp-vital ul li:last-child {border-right:none !important;}#yiv1858260842