• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

TheSeks

Blinded by the luminous glory that is David Bowie's physical manifestation.
If I understand:
Code:
decodeTheseCharacters.map(String.fromCharCode).join("");

Although browser support isn't fully fleshed out there's also TextDecoder: https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder

Javascript also supplies some tools for working directly with binary data, you can store in ArrayBuffers and read/write with DataViews.

Yeah, that's not what I'm going for now that I've looked and seen.

This is what I have:

Code:
function rot13(str) { // LBH QVQ VG!
  // Create empty array to hold character codes
  var characterCodes = [];
  // Push character codes into array
  for (var i = 0; i < str.length; i++) {
    characterCodes.push(str.charCodeAt(i));
  }
  debugger;
  // Create an empty container array to hold decoded characters
  var decodedCharacters = [];
  debugger;
  // For each character code in the array:
    for (var n = 0; n < characterCodes.length; n++) {
    // If it's less than 65 or more than 90...
    if (characterCodes[n] > 65 || characterCodes[n] < 90) {
        // Convert code to string and push to container array
        decodedCharacters.push(characterCodes[n]);
        debugger;
      }
    // Otherwise
    else {
      // Shift it by 13
      decodedCharacters.push(characterCodes[n] - 13);
      // If necessary, wrap it. If you're adding 13, wrap around 90. If subtracting, wrap around 65.
      // Convert code to string and push to container array
    }
    debugger;
    }
  // Return the contents of container array as a string 
}

rot13 ("SERR PBQR PNZC");

"debugger;" will throw up Chrome's built-in debugger. So far so good.

Array1->characterCodes will say it has 14 values (counting by 0, so 15 characters including spaces), okay. So far so good.

BUT it won't show me the values' it's pushing from ".getCharCode()" It just continues to say it's 14 values.

Then when I run the loop, it'll iterate through the loop and show me i (or n) counting up. GREAT because it means my loop is working.

THEN it returns the value of the second array (decodedCharacters). Which is great, but I want the original values as well to make sure it's working.

Why isn't Chrome giving me the two array's values in different areas/steps?

If I console.log, the arrays match to where I'm thinking decodedCharacters is throwing at characterCodes array the values it's calculating. Which isn't what I want/need. I want both of them to have their own values with originalCharacter being the before and decodedCharacters being the after so then I can breakpoint/debugger; the decoding step.
 

Makai

Member
Anyone have a good source for thinking like a programmer? Constantly I am always kind of struggling where to start or where to go next. I don't have a hard time looking at code and wondering what's going on but naturally thinking like a problem solver is difficult to me and seems to be the hardest skill to learn.
Just make your own game or website. You'll figure it out.
 

Somnid

Member
Yeah, that's not what I'm going for now that I've looked and seen.

This is what I have:

Code:
function rot13(str) { // LBH QVQ VG!
  // Create empty array to hold character codes
  var characterCodes = [];
  // Push character codes into array
  for (var i = 0; i < str.length; i++) {
    characterCodes.push(str.charCodeAt(i));
  }
  debugger;
  // Create an empty container array to hold decoded characters
  var decodedCharacters = [];
  debugger;
  // For each character code in the array:
    for (var n = 0; n < characterCodes.length; n++) {
    // If it's less than 65 or more than 90...
    if (characterCodes[n] > 65 || characterCodes[n] < 90) {
        // Convert code to string and push to container array
        decodedCharacters.push(characterCodes[n]);
        debugger;
      }
    // Otherwise
    else {
      // Shift it by 13
      decodedCharacters.push(characterCodes[n] - 13);
      // If necessary, wrap it. If you're adding 13, wrap around 90. If subtracting, wrap around 65.
      // Convert code to string and push to container array
    }
    debugger;
    }
  // Return the contents of container array as a string 
}

rot13 ("SERR PBQR PNZC");

"debugger;" will throw up Chrome's built-in debugger. So far so good.

Array1->characterCodes will say it has 14 values (counting by 0, so 15 characters including spaces), okay. So far so good.

BUT it won't show me the values' it's pushing from ".getCharCode()" It just continues to say it's 14 values.

Then when I run the loop, it'll iterate through the loop and show me i (or n) counting up. GREAT because it means my loop is working.

THEN it returns the value of the second array (decodedCharacters). Which is great, but I want the original values as well to make sure it's working.

Why isn't Chrome giving me the two array's values in different areas/steps?

If I console.log, the arrays match to where I'm thinking decodedCharacters is throwing at characterCodes array the values it's calculating. Which isn't what I want/need. I want both of them to have their own values with originalCharacter being the before and decodedCharacters being the after so then I can breakpoint/debugger; the decoding step.

I don't quite understand, you can simply hover over the array in Chrome's debugger to inspect the values at that point in time, or when it's paused you can type "decodedCharacters" in the console and it will dump it, or set a watch, or use the locals panel. It sounds like you just need to learn how to use it. Set an initial breakpoint by clicking the line number you want to stop on, refresh the page and trigger it, and use the step buttons to watch the program update line by line. Only in extremely particular cases would I ever use a debugger statement.
 

TheSeks

Blinded by the luminous glory that is David Bowie's physical manifestation.
I don't quite understand, you can simply hover over the array in Chrome's debugger to inspect the values at that point in time, or when it's paused you can type "decodedCharacters" in the console and it will dump it, or set a watch, or use the locals panel. It sounds like you just need to learn how to use it. Set an initial breakpoint by clicking the line number you want to stop on, refresh the page and trigger it, and use the step buttons to watch the program update line by line. Only in extremely particular cases would I ever use a debugger statement.

I'm trying to figure out if the array is shifting or not. It looks like it isn't so I'm not sure where I'm failing at getting the values to shift so I can then worry about decoding the character values back into a string.

But yeah, I've never used Chrome's debugger or anything so it's completely foreign and I'm just failing at trying to understand what I'm doing because to me it makes sense but apparently what I've wrote isn't clear enough to compute with.
 
Man, functional programming is weird.

its only weird if its not your first paradigm you are exposed to. I started with a functional language and OOP feels like madness to me.

I wonder if there are people who started with a language like Prolog, now that is a weird language (but also a bit familiar as it kinda feels like SQL).
 

TheSeks

Blinded by the luminous glory that is David Bowie's physical manifestation.
I'm trying to figure out if the array is shifting or not. It looks like it isn't so I'm not sure where I'm failing at getting the values to shift so I can then worry about decoding the character values back into a string.

But yeah, I've never used Chrome's debugger or anything so it's completely foreign and I'm just failing at trying to understand what I'm doing because to me it makes sense but apparently what I've wrote isn't clear enough to compute with.

Well, in the continuing saga of understanding this, I figured out the shifts.

The problem for me now is taking those array of character codes and putting them into a String.

However,
Code:
String.fromCharCode();
won't accept the entire array and decode it in one go. I'm thinking a loop would work, but I don't think doing it one at a time and having those be in the same array they're coming from would work?
 

Somnid

Member
Well, in the continuing saga of understanding this, I figured out the shifts.

The problem for me now is taking those array of character codes and putting them into a String.

However,
Code:
String.fromCharCode();
won't accept the entire array and decode it in one go. I'm thinking a loop would work, but I don't think doing it one at a time and having those be in the same array they're coming from would work?

In general avoid mutating arrays, it causes many subtle issues and if you start changing types from numbers to strings it's also really hard on the compiler and slow to perform. Use a fresh array.

The method I gave you will work. array.map(() => { ... }) is a method that takes the contents of an array and maps it using the provided function. So if you did encodedChars.map(String.fromCharCode) then it would return an array such that every element in "encodeChars" is run through String.fromCharCode, and put into a new array.

Then you have an array of chars, so use .join("") to join them as a string.
 

Makai

Member
C# kills me.

R8Vzenk.png
 

Haemi

Member
Hallo programming experts.

I started writing a fragment shader in cg and have a problem. Is there a way to get the size of the fragments or how many there are? I just want to know how the color distribution in the area of one fragment is. Like for example 30% orange, 25% green and 45% violet.

I hope you understand what i'm talking about.
 

JesseZao

Member
Ternary operator sucks in almost every language. Keep it simple.

Code:
Action<IMedia> action = null;
if (waitForVideo)
  action = loadedMedia =>
  {
    callToAction.LoadVideo.Invoke();
    OnIntroVideoCompleted(true);
  };

Maybe it's just because I started programming with it in existence, but I still prefer the ternary operator. It looks simpler to me.

I also prefer using implicit variable declaration with "var" whenever I can.
 

Ke0

Member
Messed around with Rails 5, I like some of the improvements. Oh and turbolinks 5 is actually pretty awesome surprisingly. Made a quick hybrid app with iOS that uses turbolinks 5.

Rails API is pretty awesome too, makes using frontend frameworks really painless. Haven't touched their websocket solution yet. But all in all I like Rails 5 much more than Rails 4. Like seriously, Rails API mode is just a god send...because I'm not a big JS unfortunately.

Thank you! I do think I want to actually learn PHP at some point (I used to use it when I was 12 but not extensively) but this is good for now I think.

PHP isn't super terrible just some things about it are quirky but it's just like any other language when you get down to it. I just hate the whole $ thing.

Flask is super fun, it's bolt on nature makes getting web applications up and running pretty fast. Django is like flask with a bunch of built in extensions for lack of a better word already activated. They're both fun.

its only weird if its not your first paradigm you are exposed to. I started with a functional language and OOP feels like madness to me.

I wonder if there are people who started with a language like Prolog, now that is a weird language (but also a bit familiar as it kinda feels like SQL).

Pretty much. Most people learned on OOP so functional is foreign to them, like wise people who learn on functional tend to scream at OOP.

To this day functional just doesn't click for me, then again it took me longer than most for OOP to really click.
 

Koren

Member
None, 90% of jobs are C++, Java, and C#. You might see some FP in some financial sectors and that's about it.
Aeronautics, networking, compilers, proof assistants...

Beside, I don't think prevalence is that a good metric. I'm not even convinced that C++ or Java are prevalent because they're OOP. And definitively not because OOP is the best approach for all cases.

OOP is probably an easier superset to imperative than FP will ever be, and old habits die slowly. How could FP be common? Haskell is probably too "pure" to many, OCaml too french... Give it some time, F# may very well be a good trojan for FP in the coming years.
 

Koren

Member
Kris just made up that 90/99% number. Jobs are nowhere near that concentrated in a handful of languages.
Obviously. Pure C itself may be more popular than C++ and C#, and I suspect that Java popularity in jobs has a LOT to do with mobile apps development.

And I really think that half the time, the language compagnies use is rather a result of history than reasoning. Also, libraries/framework/prior work available.


Unless you're in already in your 50s, I'd say it would be an error to not give at least FP a chance if you want to be a programmer. There's plenlty of situations where it's useful to have a different tool at your disposal.


(interestingly enough, I'm an IP guy, and even if I like FP from time to time and learned it before OOP, I use OOP more often in my projects but... most of my students, after learning IP (Python) and FP (Caml), grow fonder of FP than IP, not sure why)
 

Makai

Member
Swift has most of the functional features of the ML family and it's already a mainstream language. Lots of iOS developer jobs.
 

Slo

Member
what industries are functional styles prevalent? for mobile/web consumer facing stuff it seems all OOPy

Functional programming languages are prevalent when dealing with big data in machine learning/data science scenarios. Think large, distributed systems that analyze huge data sets in parallel and then reduce their findings to a suggestion or a score.

Retail recommendation systems, targeted ad servers, search algorithms, etc.
 
I don't care all that much about functional programming anymore. I really just care about ML and ML-likes. I found my language(s). Swift gets a nod for being in that realm.

I want strong static types, value types, algebraic data types, lambdas, no null, first class functions, the ability to use immutability as a safeguard, pattern matching, first class asynchronous/concurrent programming, and tools for composability. Whatever you want to call that, that's what I want.

Btw, Google does not use map reduce anymore: http://the-paper-trail.org/blog/the...n-horse-on-the-death-of-map-reduce-at-google/
 
Front-end web (and back-end Node) is functional even if some insist on putting an OO dressing on it.

Um, no not really, Front-end-web is filled with tons of mutable state all over the place. Up untill ES6 there wasn't even a way to declare immutable values.

Only few things on the top of my head that could be considered functional are stuff like RxJS, Elm and Redux.
 

Somnid

Member
Um, no not really, Front-end-web is filled with tons of mutable state all over the place. Up untill ES6 there wasn't even a way to declare immutable values.

Only few things on the top of my head that could be considered functional are stuff like RxJS, Elm and Redux.

But in the end you're just passing the whole state into the function via this and getting the changed state in a consistent way. The only time you have access to state that's not directly passed into the function is via shared scope.
 
Um, no not really, Front-end-web is filled with tons of mutable state all over the place. Up untill ES6 there wasn't even a way to declare immutable values.

Only few things on the top of my head that could be considered functional are stuff like RxJS, Elm and Redux.

Well, Object.freeze() has been there for a long time now. Obviously Javascript in general offer tons of ways to code from pure OO to pure functional to everything in between, but FP JS is everywhere these days (and for all the better).

I have been writing tons of Scala as of late and (not caring if someone considers this as blasphemy) pretty much 99% of the times I think that many things could be written more cleany and efficiently with Javascript in less than half of the time.
 

Koren

Member
I have been writing tons of Scala as of late and (not caring if someone considers this as blasphemy) pretty much 99% of the times I think that many things could be written more cleany and efficiently with Javascript in less than half of the time.
I won't call blasphemy, but I'm really interested in an example or two if you care, I've run away from Javascript in the past, and hearing people praising it, especially as a FP language, made me really curious...
 

Ke0

Member
Swift has most of the functional features of the ML family and it's already a mainstream language. Lots of iOS developer jobs.

I remember when Swift was first announced and many people were like "It'll never replace objC."
 
I won't call blasphemy, but I'm really interested in an example or two if you care, I've run away from Javascript in the past, and hearing people praising it, especially as a FP language, made me really curious...

Well, it's not specifically a FP problem, but for example I really think that the Option type sucks more than it benefits the developer, which forces the developer to do endless flatMaps and pattern matching and crap to retrieve stuff that a simple null check would solve in single line.

Not only that, but in Scala there's always 50 ways to do a thing, and not a single one of those is exactly the defacto way. It applies to JS too, with the difference that there's usually one preferred way and then 50 ways to do it the hard way. Scala, in my mind, is way too flexible for it's own good, which makes it too easy to create code that's too verbose and hard to read, even if the solution is rather magical. Not to mention that the language moves rather fast but the libraries and especially the documentation is fucking terrible almost always.

And best of all, the compiler is so goddamn slow that most of the days you spend ages on stuff compiling, instead of doing actual work. Hopefully Dotty saves us.

We are building an application (well, a collection of micro services that as whole is an application) of major scale, and while I acknowledge that Scala could be an saviour in case X or Y, I constantly think that a problem X or Y could be easier to solve in other place than in code (for example in the infrastructure instead).

Some of this might be skewed by the fact that it's a Play Framework (+ Slick etc) app, that for reals might the most overbearlingly heavy framework I have worked in my life, despite having worked with tons of monolith solutions.

Modern Javascript on the other hand provides you tons of lightweight solutions with all the necessities on doing pure FP.

TL;DR: Scala is a strong language with amazing set of tools build in that are too easy to abuse. Javascript is a strong language with amazing set of tools build around it that are also too easy to abuse, but due the popularity of the language there's tons of structure around it or something.
 
Well, it's not specifically a FP problem, but for example I really think that the Option type sucks more than it benefits the developer, which forces the developer to do endless flatMaps and pattern matching and crap to retrieve stuff that a simple null check would solve in single line.
Thumbs down. Tony Hoare wouldn't call it the billion dollar mistake if it wasn't as bad as that sounds. Total functions are an ideal that should almost never be sacrificed. If it can be "nothing", let the record show as much! Doesn't Scala have operators for flatmap and friends?

Not only that, but in Scala there's always 50 ways to do a thing, and not a single one of those is exactly the defacto way. It applies to JS too, with the difference that there's usually one preferred way and then 50 ways to do it the hard way. Scala, in my mind, is way too flexible for it's own good, which makes it too easy to create code that's too verbose and hard to read, even if the solution is rather magical. Not to mention that the language moves rather fast but the libraries and especially the documentation is fucking terrible almost always.
The curse of not being a huge enterprise language. I don't know of a non-mainstream language where this isn't true.

But if you really want to tell me about the quality of JavaScript libraries, all I have to do is point to left-pad. I feel like that's a pretty complete argument on its own.

And best of all, the compiler is so goddamn slow that most of the days you spend ages on stuff compiling, instead of doing actual work. Hopefully Dotty saves us.

I agree with this one. Scala is abyssmal here.
 
But if you really want to tell me about the quality of JavaScript libraries, all I have to do is point to left-pad. I feel like that's a pretty complete argument on its own.

You mean a library that had a 100% test coverage, was battle tested by millions of real life applications and had excellent documentation?
 
You mean a library that had a 100% test coverage, was battle tested by millions of real life applications and had excellent documentation?
It was 11 lines, one function, and was a package on its own. It broke, and suddenly many, many big name projects started breaking, too. That sounds awfully brittle and chaotic to me.
 

Two Words

Member
Functional programming is very new to me. I'm working on an assignment in Racket and I can't figure out what is wrong. Debugging feels a lot harder in functional programming since it is so reliant on recursion. Here's my code:

Code:
(define my-map
  (lambda (funct lst)
    ((if (null? lst)
         ('())
         (cons (funct (car lst)) (my-map funct (cdr lst)))
    ))

  )    
)

If you had a list called x and said (my-map sqr x), you should expect it to return a list where each element in x was squared. You can assume that each element in the list is an atom, and not a list. So no sublists. And you can assume that every element in the list works for the function being passed as an argument. This is the error that I am getting.

application: not a procedure;
expected a procedure that can be applied to arguments
given: '()
arguments...: [none]
> (sqr 5)
 
Functional programming is very new to me. I'm working on an assignment in Racket and I can't figure out what is wrong. Debugging feels a lot harder in functional programming since it is so reliant on recursion. Here's my code:

Code:
(define my-map
  (lambda (funct lst)
    ((if (null? lst)
         ('())
         (cons (funct (car lst)) (my-map funct (cdr lst)))
    ))

  )    
)

If you had a list called x and said (my-map sqr x), you should expect it to return a list where each element in x was squared. You can assume that each element in the list is an atom, and not a list. So no sublists. And you can assume that every element in the list works for the function being passed as an argument. This is the error that I am getting.
It looks like you're evaluating too many times. Does this work?

Code:
(define my-map
  (lambda (funct lst)
    (if (null? lst)
        '()
        (cons (funct (car lst)) (my-map funct (cdr lst)))
    )

  )    
)
 

Two Words

Member
It looks like you're evaluating too many times. Does this work?

Code:
(define my-map
  (lambda (funct lst)
    (if (null? lst)
        '()
        (cons (funct (car lst)) (my-map funct (cdr lst)))
    )

  )    
)

It seems like all you did was remove a set of parenthesis around the if block and the first case for he if block. But wasn't that additional parenthesis set around the if block meant to be around whatever the lambda expression represented?


Edit: Nevermind about the output format. That was just due to the length of the list.
 
Yes, but the output looks like this for some reason.

Code:
'(0
  4
  16
  36
  64
  100
  144
  196
  256
  324
  400
  484
  576
  676
  784
  900
  1024
  1156
  1296
  1444
  1600
  1764
  1936
  2116
  2304
  2500)
>

It seems like all you did was remove a set of parenthesis around the if block and the first case for he if block. But wasn't that additional parenthesis set around the if block meant to be around whatever the lambda expression represented?
That's probably just how your REPL prints long lists, which seems reasonable to me.

The parentheses are not syntax for functions. I repeat! The parentheses are not syntax for functions. Long explanation:

lambda is a special form which takes two arguments: a list of symbols to be bound in the environment, and a single expression to be evaluated in the new execution environment. Let's look at your code again:

Code:
(lambda (funct lst)
    ((if (null? lst)
         ('())
         (cons (funct (car lst)) (my-map funct (cdr lst)))
    ))

  )

You've got the argument list just fine here, but look at the next piece. Here's the expression you have written down:

Code:
((if (null? lst)
         ('())
         (cons (funct (car lst)) (my-map funct (cdr lst)))
    ))

The if form is actually incorrect too, but ignoring that, let's say it evaluates correctly to the list (1 4 9 16). Now let's see what our expression looks like:

Code:
((1 4 9 16))

Oh no! We are trying to evaluate our list as a function of no arguments. Not good. So that's what I mean when I say you're overevaluating.

It's easy enough to see why trying to evaluate ('()) is a bad idea, too. Happy scheming!
 

Two Words

Member
That's probably just how your REPL prints long lists, which seems reasonable to me.

The parentheses are not syntax for functions. I repeat! The parentheses are not syntax for functions. Long explanation:

lambda is a special form which takes two arguments: a list of symbols to be bound in the environment, and a single expression to be evaluated in the new execution environment. Let's look at your code again:

Code:
(lambda (funct lst)
    ((if (null? lst)
         ('())
         (cons (funct (car lst)) (my-map funct (cdr lst)))
    ))

  )

You've got the argument list just fine here, but look at the next piece. Here's the expression you have written down:

Code:
((if (null? lst)
         ('())
         (cons (funct (car lst)) (my-map funct (cdr lst)))
    ))

The if form is actually incorrect too, but ignoring that, let's say it evaluates correctly to the list (1 4 9 16). Now let's see what our expression looks like:

Code:
((1 4 9 16))

Oh no! We are trying to evaluate our list as a function of no arguments. Not good. So that's what I mean when I say you're overevaluating.

It's easy enough to see why trying to evaluate ('()) is a bad idea, too. Happy scheming!

Ahh, I see now. Thanks for the explanation. I think I was erroneously associating () too closely to { } for scope in imperative languages. I do wonder if I'm hurting my way of thinking in functional programming when I write my code in such a way that ( ) look similar to how one would use { } in an imperative language. For example, another problem I had to do was create my own reverse function. I was able to make that one on my first try this time and it looks like this. See how the parenthesis are placed? Am I sort of forcing myself to evaluate problems imperatively when I structure it like this or is this the norm?

Code:
(define my-reverse
  (lambda (lst)
    (my-reverse-helper lst '())))

(define my-reverse-helper
  (lambda (lst accumulator)
    (if (null? lst)
        accumulator
        ( my-reverse-helper(cdr lst) (cons (car lst) accumulator)  )
    )
  )
)
 
Ahh, I see now. Thanks for the explanation. I think I was erroneously associating () too closely to { } for scope in imperative languages. I do wonder if I'm hurting my way of thinking in functional programming when I write my code in such a way that ( ) look similar to how one would use { } in an imperative language. For example, another problem I had to do was create my own reverse function. I was able to make that one on my first try this time and it looks like this. See how the parenthesis are placed? Am I sort of forcing myself to evaluate problems imperatively when I structure it like this or is this the norm?

Code:
(define my-reverse
  (lambda (lst)
    (my-reverse-helper lst '())))

(define my-reverse-helper
  (lambda (lst accumulator)
    (if (null? lst)
        accumulator
        ( my-reverse-helper(cdr lst) (cons (car lst) accumulator)  )
    )
  )
)
The parenthesis placement is definitely not affecting the way you code. But your style is also abnormal. Most people read indentation, not parens, and the indentation style is "Whatever Emacs says". :p

I don't know what "functional" means anymore. I really don't. Is it just first class functions? Because unless recursion makes the problem easier to understand, it's kind of a stupid party trick. Look, ma, no state.

Btw, are you aware of the special syntax for define?

(define (myFun arg1 arg2) (+ arg1 arg2))
 

Two Words

Member
The parenthesis placement is definitely not affecting the way you code. But your style is also abnormal. Most people read indentation, not parens, and the indentation style is "Whatever Emacs says". :p

I don't know what "functional" means anymore. I really don't. Is it just first class functions? Because unless recursion makes the problem easier to understand, it's kind of a stupid party trick. Look, ma, no state.

Btw, are you aware of the special syntax for define?

(define (myFun arg1 arg2) (+ arg1 arg2))

I thought define is meant to associate a name to an expression or function of some sort. So when I define my-reverse or my-map, I'm associating that name to a lambda expression and those names expect the variables listed in the lambda expression.


Also, I'm slightly worried that I'm solving all of my solutions using tail recursion :p
 
I thought define is meant to associate a name to an expression or function of some sort. So when I define my-reverse or my-map, I'm associating that name to a lambda expression and those names expect the variables listed in the lambda expression.


Also, I'm slightly worried that I'm solving all of my solutions using tail recursion :p
You are, and that sucks in the long run, so there's a shorthand for it.
 

Two Words

Member
I think I'm okay with recursion for the most part, but it just seems like tail recursion is an easy solution many times. I think I'm just making a habit of going "well, how would I do this if I had loop structures? Well, I can easily do that with tail recursion like so..." and just dance around making it recursively build the solution on the way back from the base case.
 

Gurrry

Member
Hey guys im currently taking my first ever computer science class and were learning Python. Ive finally come across my first bump in the road and im not sure how to get this working the way our professor wants.

Is there anyone that may be able to help me? Its a program that mainly deals with outer/inner loops or nested loops. Im sure it will be a piece of cake for anyone that has more experience in the language.

My problem right now is that I keep getting this "list index out of range" error. Im trying to iterate through a list with a for loop. Im defining a variable called "rangeTemp" and setting it = to 0 outside of my loop. For each iteration through the inner loop, im adding 1 to it.

However, the problem seems to be that it keeps skipping the first number within the range. For instance, rangeTemp starts at 0, then it gets added 1 to it, therefor my range would be 1. But its basically skipping 1 and then giving me an error.

This is probably a horrible explanation. I can post the code if its allowed for more clarity.
 

Gurrry

Member
Post code. Loop iterators is a common thing to trip over for new programmers.

Yeah man, no matter what language ive tried to learn, loops constantly fuck me up.

Basically, the goal here is to have the user enter 5 stores. Its going to print the store name, address, and number, then list out all of the information they enter during the loop (vegetable name, vegetable PLU, and vegetable order ammount).

The output needs to look like this

Austin Store
555-555-5555
123 Address Lane

Vegetable 1 - Banana
Vegetable PLU 1 - 123123123
Vegetable 1 Order - 56

Vegetable 2 - Pickle
Vegetable 2 PLU - 123123123
Vegetable 2 Order - 100

etc.

Then loop back to the beginning, ask for the next store name, adress, number and then do the whole thing again.
 
Top Bottom