3

I am a self-taught, novice programmer. The language I chose was Objective-C, so that I can write Apple applications and software. I have learned the hard way that Objective-C has a steep learning curve, but I have forced myself to look at it over and over until I get it right.

I feel I am completing one major step, and that is becoming very comfortable with the syntax. Now as I enter the next big step - finding out what is possible with a computer programming language - I am thinking that all software is just developed within the confines of:

  • classes
  • loops
  • booleans
  • IF/OR statements
  • variables
  • etc.

Is that a true assessment? When thinking of software in my head do I just need to learn to convert it all to the above? Right?

Sorry if this question sounds funny, but I am just learning how to take the language in and talk (what I think is) the language of a computer. Is there a good book or tutorial that helps illustrate how to "speak as a computer would"?

11
  • 6
    I had a vision of Scotty speaking into a mouse.
    – MathAttack
    Mar 24, 2012 at 17:14
  • 5
    1001111011100011001
    – user7007
    Mar 25, 2012 at 1:21
  • here is a good place to start if you want a bit of history and you can dig down into how electricity is used to model states that we can ascribe meaning to in order to create a computer.
    – Ryathal
    May 8, 2012 at 21:30
  • 2
    "Hey computer, wassup?" May 9, 2012 at 3:06
  • That's actually a very good "soft" question! Those are too rare. May 9, 2012 at 5:36

2 Answers 2

18

Fundamentally, computers can only do a few basic things:

  1. They can perform simple arithmetic
  2. They can execute instructions sequentially
  3. They can branch to another instruction, based on a condition
  4. They can loop (a variation of branching)
  5. They can move data from one place to another
  6. They can store data and retrieve it for later use.

That's about it. Everything you see a computer do is just an elaborate abstraction over these simple operations.

Programming languages are one of those abstractions. They allow us to speak the computer's language (technically, machine code), using a symbolic language that is closer to the language we humans use to communicate. Programs called compilers translate programming languages into machine code, so that the machine can execute our instructions, just as we specified them.

There are many different computer languages, each optimized for a particular way of thinking or problem solving. These ways of thinking are called paradigms; many programming languages support more than one paradigm. Objective C is just one such language.

The way each computer programmer gets started is as individual as your fingerprints. If you really want to "get" the idea of computers, my advice is to learn more than one language. Study other languages; see how they differ. See how they all have certain things in common; although they might solve a problem differently, at their core they still resolve down to the same fundamental operations.

4
  • Well said... :-) Mar 24, 2012 at 6:26
  • Wow! That was a great reply - Thank you for the explanation. It makes me really excited to learn another and then another language. Thanks!
    – SalemSeven
    Mar 24, 2012 at 21:09
  • You forgot to add: "They can store data and retrieve it for later use."
    – Bernard
    Mar 24, 2012 at 23:07
  • Then can also execute some instructions in parallel, depends on the processor(s) and the instructions. May 9, 2012 at 11:12
1

First-Class Functions

Another useful feature that a programming language can have is first-class functions. A first-class function is a function that can be used as data.

In a language in which you can treat functions as data, you can create variables that refer to them, pass them around as arguments to functions, and return them from functions. Once case where you might use this is the famous map function.

Say that I just got some string from a file or something and that the string contains a series of integers separated by spaces. Say I use a string-splitting function to get an array of strings containing each of the integers (still as strings). I have access to a library function to parse an integer from a string, and I want to use it to convert each of these strings containing integers to actual integer values. I create the new array and write a loop to convert each string and store the resulting integer in the new array.

Now say I want to take a different array of integers and find the absolute value of each and store the results in a new array. I can use another loop to do this. However, I have this pattern showing up in my code: start with an array, perform some transformation of each value in the array, and store all of the results in a new array. I could just write out the code to do this each time, but there is a way to abstract out the process of creating the new array, looping, transforming, and storing.

I can write a function that takes as arguments

  • an array of type x
  • a function for somehow transforming objects of type x into objects of type y (possibly the same type as x)

and have it create an array of elements of type y of the correct length, loop over the values in the original array, call the function argument to do the transformation, store the results in the new array, and return the new array.

After I've written this function, map, I don't have to write this code out by hand any more.

Objective-C should have function pointers because C has them.

The way I described map also took advantage of some sort of generic typing.

Closures

Another feature is closures. A closure is a function object that captures some of the variables that existed in the scope in which it was created. What does that mean?

Some languages let you write function literals, functions that don't (necessarily) have names and just appear in you code without a special separate declaration. When you write a function literal in another function, there might be variables in the same scope as the function literal. There are situations where you might find it useful to use those variables inside of the function literal and have those variables stay with the function object. Such a function object would be a closure.

Here's an example of code that I wrote that creates a closure (written in Common Lisp):

(defun exponential-change (function constant)
  (lambda (time)
    (* (expt constant time) (funcall function time))))

(Don't ((let (the) parentheses) scare you)). Think of it as being written like this:

function exponential_change (function, constant) {
    return function (time) {
        return exponent(constant, time) * function(time);
    };
}

I wrote this for synthesizing sound. I represent a sound as a function of time that returns amplitude samples (-1 to 1). This function, exponential-change, transforms a sound by making the amplitude increase or decrease (depending on the constant) in an exponential fashion. exponential-change creates returns a function of time based on another function of time. The fact that the new function keeps track of what function it needs to modify prevents other code from having to keep track of that detail.

Closures not only keep track of the values of the variables they capture but also the variables themselves. This means that you could create a function that modifies those variables.

Not the answer you're looking for? Browse other questions tagged or ask your own question.