Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Scaramouche posted:

That said, I look at the programming I do now, day in and day out, and just don't see how FP would do it. I can bet that the stuff it can do well is pretty awesome (which, I'm guessing would involve pure math and analytics), but tweaking web layouts, database massaging, file parsing, etc. doesn't seem to fit the bill.

Which leads me to a question; has anyone been exposed to both Miranda and Haskell and can tell me how similar they are? Wikipedia says 'very similar' but doesn't really give specifics, but I'd be curious to know how much changeover would be involved if I wanted to pick up Haskell based solely on my Miranda exposure (I got to be pretty good at it at the end).
File parsing is one of functional programming's strong points -- libraries like Haskell's attoparsec can approach the performance of hand-written C, don't need yacc-style pregeneration, and are very easy to maintain.

Databases are a weak point, since binding most DB libraries is a giant pain in the rear end. If you just need to generate/run SQL you can use HDBC or HSQL, but they've not kept up with modern Haskell style and can be a bit gnarly to work with.

Not sure how you'd use a programming language to tweak web layouts; if you're just want to change fonts or colors or something, you can treat Haskell like a strongly-typed Perl, but I don't see much advantage to it.

----

Miranda is one of Haskell's ancestral languages; Haskell adopted several features from it, and some of the syntax will likely be familiar. However, from reading Wikipedia, it doesn't have Haskell's support for stateful programming. Features like manual memory management, pointers, or the FFI will be new to you.

The best book to read for anyone interested in Haskell as a real programming language is Real World Haskell. The Haskell community has been improving at an incredible rate, so there's a bit of adjustment needed to work with modern libraries, but the fundamental teaching is sound.

Adbot
ADBOT LOVES YOU

Milotic
Mar 4, 2009

9CL apologist
Slippery Tilde

Janin posted:

Imperative/declarative is a matter of syntax. Haskell supports both declarative (let-notation) and imperative (do-notation).

I disagree, but I don't want to press this point, since it will ultimately come down to whatever dictionary definition, university text-book you read. I would point out that unfortunately the haskell.org wiki is a bit schizophrenic about whether it is an imperative language is not - saying it is in one article, then not in another.

Janin posted:

Any tutorial/introduction/book about Haskell that doesn't explicitly mention using it as an imperative language (as a replacement for C) is terribly incomplete. For example, I've never even heard of the book you linked as a "bible", and if it was published in 1998(!!) then it's basically worthless for all intents and purposes. I mean, gently caress, that means it was written before Haskell had modules! Throw that poo poo out.

Throw that out? No way man. There was a third edition out, but I was poor at uni so couldn't afford it and the third edition was always checked out of all the libraries.

I would welcome suggestions on alternatives though. I read it from time to time on the train to stretch the mind, but it could be better written.


quote:

If by "finance sector" you mean anyone employed by any financial company, then sure -- most of them are probably using something like COBOL or Java. But if you mean people actually involved in writing finance-related code, almost all of it will be in some sort of functional language. Whether that's ML, OCaml, Matlab, R, Haskell, doesn't really matter.

That's pretty much what I mean. I wouldn't have included Matlab as a 'pure' functional language, but YMMV.

shrughes
Oct 11, 2008

(call/cc call/cc)
Haskell is basically a fun language to learn, and a dysfunctional language to use. It's impossible to think of something where you'd decide to use Haskell for that, because it's the best choice. This is because good strictly evaluated functional programming languages exist. The only real use of laziness is to avoid petty verbosity, not increase safety. The only reason to use Haskell is because it's a statically typed language with an awesome runtime, except, oh wait, even then the fact that it's lazily evaluated ruins everything.

I have of course been completely unfair to Milotic, because that's just my personality -- it took me more than a semester or two to "get" Haskell, and it took me even longer to appreciate how awful it is. And I didn't have bad books to teach it to me.

shrughes
Oct 11, 2008

(call/cc call/cc)

Milotic posted:

I would welcome suggestions on alternatives though. I read it from time to time on the train to stretch the mind, but it could be better written.

Purely Functional Datastructures is a nice read and has practical uses for nonfunctional programming.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Milotic posted:

Throw that out? No way man. There was a third edition out, but I was poor at uni so couldn't afford it and the third edition was always checked out of all the libraries.

I would welcome suggestions on alternatives though. I read it from time to time on the train to stretch the mind, but it could be better written.
Then turn it into kindling, or use it as a doorstop, or whatever. Reading a bad book is worse than reading nothing.

Read this: Real World Haskell. You can buy the physical version, or download the online version for free.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

shrughes posted:

Haskell is basically a fun language to learn, and a dysfunctional language to use. It's impossible to think of something where you'd decide to use Haskell for that, because it's the best choice. This is because good strictly evaluated functional programming languages exist. The only real use of laziness is to avoid petty verbosity, not increase safety. The only reason to use Haskell is because it's a statically typed language with an awesome runtime, except, oh wait, even then the fact that it's lazily evaluated ruins everything.
Why on earth would you ever want a strictly evaluated language? Particularly a functional one? It seems like whenever I have to use a strict language, half of my effort is spent just getting it to stop evaluating the whole world for every little value.

JewKiller 3000
Nov 28, 2006

by Lowtax

Janin posted:

Why on earth would you ever want a strictly evaluated language? Particularly a functional one? It seems like whenever I have to use a strict language, half of my effort is spent just getting it to stop evaluating the whole world for every little value.

Um, because you want it to be obvious when your expressions are evaluated, so that you can reason about the behavior of your code particularly in the presence of side effects (which exist if your program interacts with the outside world). If you want to delay the evaluation of some expression, then you make it the body of a function, and call the function when you want the value... what's the big deal? OCaml has lazy support built in, but it's a bad default. Even Simon PJ openly admits this.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

JewKiller 3000 posted:

Um, because you want it to be obvious when your expressions are evaluated, so that you can reason about the behavior of your code particularly in the presence of side effects (which exist if your program interacts with the outside world).

If only there were a way to separate side effects from the rest of the code. We could even say that the code without side effects was "pure", and that functions guaranteed not to have side effects were called "pure functions". If only that were possible, then lazy evaluation would be useful, since it doesn't matter when your code is actually executed in the absence of side effects.

But I guess that's impossible. Clearly, side effects are completely inseparable from all code. After all, without side effects our code is useless. No one could ever possibly invent a construct that allowed the language to be capable of side effects AND have pure functions. That's just insane.

shrughes
Oct 11, 2008

(call/cc call/cc)

Ryouga Inverse posted:

But I guess that's impossible. Clearly, side effects are completely inseparable from all code. After all, without side effects our code is useless. No one could ever possibly invent a construct that allowed the language to be capable of side effects AND have pure functions. That's just insane.

Ahem, the downside of lazy evaluation is performance, especially the kind where you use arbitrarily large amounts of memory and start thrashing.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

JewKiller 3000 posted:

Um, because you want it to be obvious when your expressions are evaluated, so that you can reason about the behavior of your code particularly in the presence of side effects (which exist if your program interacts with the outside world). If you want to delay the evaluation of some expression, then you make it the body of a function, and call the function when you want the value... what's the big deal? OCaml has lazy support built in, but it's a bad default. Even Simon PJ openly admits this.
It is obvious when your expressions are evaluated -- it's whenever their value is needed. When interacting with the outside world, all values are strict unless you're doing something really crazy (in which case all bets are off).

shrughes posted:

Ahem, the downside of lazy evaluation is performance, especially the kind where you use arbitrarily large amounts of memory and start thrashing.
The "lazy evaluation causes unbounded memory use" myth is a bit like "dynamic typing causes crashes everywhere"; a theoretical possibility, but in practice very unusual.

shrughes
Oct 11, 2008

(call/cc call/cc)

Janin posted:

It is obvious when your expressions are evaluated -- it's whenever their value is needed. When interacting with the outside world, all values are strict unless you're doing something really crazy (in which case all bets are off).

The "lazy evaluation causes unbounded memory use" myth is a bit like "dynamic typing causes crashes everywhere"; a theoretical possibility, but in practice very unusual.

No it's not very unusual, and it's not a theoretical possibility, it happened a lot for me and ShoulderDaemon was bitching about it the last time we talked about it, and the last time I checked out a Haskell web server benchmark it failed to complete the highest load because it started thrashing. Maybe you just don't try to use Haskell for sufficiently interesting or sufficiently data-intensive stuff.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

shrughes posted:

No it's not very unusual, and it's not a theoretical possibility, it happened a lot for me and ShoulderDaemon was bitching about it the last time we talked about it, and the last time I checked out a Haskell web server benchmark it failed to complete the highest load because it started thrashing. Maybe you just don't try to use Haskell for sufficiently interesting or sufficiently data-intensive stuff.
I've written a few dozen large libraries, mostly for long-lived high-throughput server applications, plus some of my libraries serve as the foundation for two popular Haskell web servers (Warp + recent releases of Snap) and a major web application (Hoogle).

In the years I've spent working in Haskell, I have never written code that began consuming arbitrarily large space, and can count the number of small (= few hundred kB/hour) space leaks on one hand.

If you are regularly writing code of which you don't understand the space and/or time performance, then you should try learning about how to use your tools.

tef
May 30, 2004

-> some l-system crap ->

quote:

Why on earth would you ever want a strictly evaluated language? Particularly a functional one? It seems like whenever I have to use a strict language, half of my effort is spent just getting it to stop evaluating the whole world for every little value.

Janin posted:

then you should try learning about how to use your tools.

never change

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

tef posted:

never change
In strict languages, learning how to work around strictness is part of learning the tools.

For example, using self-populating proxy lambdas and scoped imports in Python, or procedure-local static initialization instead of global variables in C++.

zootm
Aug 8, 2006

We used to be better friends.

shrughes posted:

Purely Functional Datastructures is a nice read and has practical uses for nonfunctional programming.
As much as I like this book, it's hard to argue that the effort/reward ratio is a good one for non-academic uses. A workmate of mine has pointed out that if you don't care too much about proofs and don't feel like figuring out which parts are safe to skim it's not necessarily a fun book to read.

Tux Racer
Dec 24, 2005

Eggnogium posted:

I was reviewing a buddy of mine's homework for an intro CS class I convinced him to take. He was asked to get a bunch of information from a '/' delimited string and store it an array.

Since the class is taught with Java, the correct solution would have been String.split(). Instead he came up with this ugly workaround that made me laugh:

code:
Scanner scan = new Scanner(args[0]);
scan.useDelimiter("/");
ArrayList<String> list = new ArrayList<String>();

while(scan.hasNext())
  list.add(scan.next());

String[] info = new String[list.size()];

for(int i = 0; i < list.size(); i++)
  info[i] = list.get(i);

Unless there were no spaces in the file, which we all know is bad thing to assume, no String.split() would not have been to correct way to do it. Had there been spaces, he would have gotten one token and not the whole file.

EDIT: Also, the way it was explained to me by a professor recently, our intro to programming class has less to do with learning a language or programming in general, but learning logic. That's why a lot of schools use Java since it's easy to learn.

Tux Racer fucked around with this message at 12:19 on Mar 9, 2011

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

Tux Racer posted:

Unless there were no spaces in the file, which we all know is bad thing to assume, no String.split() would not have been to correct way to do it. Had there been spaces, he would have gotten one token and not the whole file.

What? It didn't say anything about a file, it said from a '/' delimited String. Even if it was a file, you'd have to read it into a String first anyway, which has nothing to do with whether .split() works as it should.

Frozen Peach
Aug 25, 2004

garbage man from a garbage can

Tux Racer posted:

EDIT: Also, the way it was explained to me by a professor recently, our intro to programming class has less to do with learning a language or programming in general, but learning logic. That's why a lot of schools use Java since it's easy to learn.

Exactly. I'm more impressed that the dude managed to figure out a hard-coded way of doing it rather than resorting to Split(). Sure split is a great function and 99.9% of coders used to the language will use it, but using it doesn't show a grasp of the actual logic that a developer should be using for more difficult tasks. Making someone think like that to make their own algorithm is, in the long run, a much more valuable thing to learn than just following the language.

Tux Racer
Dec 24, 2005

Kilson posted:

What? It didn't say anything about a file, it said from a '/' delimited String. Even if it was a file, you'd have to read it into a String first anyway, which has nothing to do with whether .split() works as it should.

Oops, I misread. I still don't think that code is anything to scoff at. It's a fairly clean solution to a problem and the way he did it was really good.

Eggnogium
Jun 1, 2010

Never give an inch! Hnnnghhhhhh!
I wasn't trying to disparage it. Obviously he's just getting used to programming and the different functions that are available to the programmer from the get-go. Like I said, the code made me laugh, not cry, in part because, in my opinion, Scanner.useDelimiter() ought to be much more obscure to a novice than String.split().

Tux Racer
Dec 24, 2005

Eggnogium posted:

I wasn't trying to disparage it. Obviously he's just getting used to programming and the different functions that are available to the programmer from the get-go. Like I said, the code made me laugh, not cry, in part because, in my opinion, Scanner.useDelimiter() ought to be much more obscure to a novice than String.split().

Nah. It was probably one of those things where he found that first, so that's what he uses. Back in high school we used StringTokenizer to do the same thing because that's what we found. I still use it even though I know about String.split() and Scanner.useDelimiter().

HFX
Nov 29, 2004

Tux Racer posted:

Nah. It was probably one of those things where he found that first, so that's what he uses. Back in high school we used StringTokenizer to do the same thing because that's what we found. I still use it even though I know about String.split() and Scanner.useDelimiter().

StringTokenizer is very backwards compatible. I even have to use it when I'm writing for certain platforms because nothing else is available.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
It's also a hell of a lot easier to read quoted strings with a StringTokenizer than with a Scanner.

indulgenthipster
Mar 16, 2004
Make that a pour over
edit: Ok it's not that bad, carry on.

indulgenthipster fucked around with this message at 17:18 on Mar 10, 2011

Tux Racer
Dec 24, 2005
Speaking of Java, I came across a lovely quirk. I was writing an assembler for a class using Java. It didn't have to actually produce runnable object code, so we were just to throw it into a text file using raw text. In the source file that we were to run this against, my professor used tabs a lot. Along with spitting out the object program to a file, we had to print out the source in one column and the corresponding object code in the other column. So I just do a System.printf("%35-s%6s", sourceLine, objCode) in order to format it correctly. I run the program and get something like this:

code:
Source Listing                 Code
SYMBOL      OP                     FD2345
      OP2      SYMBOL                   AD123
After about an hour of checking every source I could find about String.format(), I copy/pasted a couple of lines into notepad and noticed that there weren't an equal number of characters in each line. Turns out String.format() would count a tab as one character and output that to the command line, and that would interpret a tab as a certain number of characters. I would have expected this from C or C++, but Java? I ended up having to break each line of source coce into individual strings and format it that way.

Sebbe
Feb 29, 2004

Tux Racer posted:

Turns out String.format() would count a tab as one character and output that to the command line, and that would interpret a tab as a certain number of characters.

Tabs are one character.

I'd say that String.format() assuming a specific tab-width would be much worse.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Yeah, I'm confused as to why you would expect a tab not to count as one character.

Tux Racer
Dec 24, 2005
Because printf is used to print out text formatted a specific way. I dunno, but I just feel like there should be some recognition of "oh this is going directly to the terminal. I should probably make sure tab isn't loving up the spacing." I agree that String.format should keep the tabs in there. I should have said printf.

Also, I didn't expect it because i didn't think about it. I made the assumption that it would see a tab and think "oh that's going to gently caress this up." There also isn't anything in the printf or the String.printf listing in the API that would indicate a tab is only counted as 1 character. Yes it is a foolish assumption, but not having even an option to fill in tabs with x number of spaces seems dumb. I even tried String.replaceAll() and that didn't work.

EDIT: Yeah it was kind of a foolish assumption, but I still maintain the option should be there. It's 2011.

EDIT 2: Changed wording for clarity.

Tux Racer fucked around with this message at 21:18 on Mar 10, 2011

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
Since you're formatting to a fixed-length anyway, just .trim() the strings you're giving to the format string and all those problems go away, don't they?

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Tux: You seem rather quick to blame your tools for what seems like a really straightforward misunderstanding of how things work. Libraries shouldn't bend over backwards to keep you from forgetting that tabs are characters, too. Try to imagine the problems your suggestion would cause if you wanted to print out an actual tab.

String.replaceAll() works just fine for me:
code:
String src = "\ta \tb";
String dst = src.replaceAll("\t", "   ");

System.out.format("'%s'%n", src);
System.out.println(src.length());
System.out.format("'%s'%n", dst);
System.out.println(dst.length());
code:
'	a 	b'
5
'   a    b'
9

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
A tab is one character and you use tabs all the time to format text for output to a terminal. A tab can be defined by the viewer to be an arbitrary number of spaces; in general it's useful for formatting tables where you don't have to count the number of spaces between the columns. Every programming language in existence treats a tab as a single character because that is what a tab is.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Ryouga Inverse posted:

A tab can be defined by the viewer to be an arbitrary number of spaces

Don't tabs mean "go to the next tab stop", and it's the tab stops that are defined as an arbitrary number of spaces apart?

[/pedantry]

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Not treating tabs as a single character would be something that would fit in this thread.

1337JiveTurkey
Feb 17, 2005

It's more of an irk than anything else, but the Scala compiler treats all tabs as eight spaces and I've got IDEA set up with 4 space tabs so all of the error messages end up pointing to the wrong spot on the line.

Tux Racer
Dec 24, 2005
Like I said, it was a moment of realizing that tabs are a single character and this viewed it as a single character. Yeah that's a misunderstanding of the tools. I still maintain, like I said in my previous post, that there should be an option with String.format() that would prevent this from happening. I recognize that tabs can be useful, which is why there should be some sort of option. No, String.trim() wouldn't have worked in this situation. I am recognizing that I hosed up, and I learned something from it (which is what should happen when someone fucks up), but converting a tab to spaces as to not gently caress up the formatting really isn't a huge deal. I wouldn't really call that having the API bend over backwards for me.

Not realizing that it wold count the tab as one character was a bonehead move on my part. I am owning that. To my defense though, there is nothing in the API that says it formats it based on characters, not spacing.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Tux Racer posted:

converting a tab to spaces as to not gently caress up the formatting really isn't a huge deal

So do it. Problem solved.

Dren
Jan 5, 2001

Pillbug

Tux Racer posted:

I still maintain, like I said in my previous post, that there should be an option with String.format() that would prevent this from happening.

NO

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Alllll right, finally some religious discussion!

:munch:

McGlockenshire
Dec 16, 2005

GOLLOCKS!
I'll just leave this here.
code:
SELECT GROUP_CONCAT(array_row SEPARATOR '~')
FROM (SELECT CONCAT(sumtime,':',title,':',url) A.array_row
FROM (SELECT SUM(time_spent) AS sumtime, title,url
FROM library WHERE delete_status = 0
GROUP BY url_id ORDER BY sumtime DESC) A) AA;
Best guess on what it's doing gets a free :psyboom: for their troubles.

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Is the first thing that gets done to the result of that query a split on `~'?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply