Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby korona » Thu Jul 19, 2012 10:54 pm UTC

Yakk wrote:Yes.

What I want is a C/C++ level language with the ability to do compile time introspection of class type structures, and metaclasses of classes with access to that.

So I can say "GarbageCollected class foo", which causes compile-time parsing of the methods/variables of foo. It can, for example, check that each data member of foo is also GarbageCollected. Similarly, when the foo type is used (not the instances, the type), it can validate the context (so raw pointers to foo aren't allowed). In effect, I want to be able to in-language have enough power to create a Java/C# esque "managed" typesystem.

Similarly, I want to be able to say "Reflecting class foo", and have it make the members of foo Reflect. Or "Property class foo", and when I create Property members their access has implicit access to the this pointer (in effect, I modify both code in foo, and code that uses foo). But I want this to happen with zero run-time cost, a modest compile-time cost, and with a system strong enough to do these kind of things in the language without it being a bunch of hard-coded keywords that only do this (because I don't know what else could be done this way..).

Then I can have a GC GUI low-performance reflectable system embedded adjacent to my high-performance audio filtering, without having to deal with crossing a language barrier half way.


That is exactly the reason why always wanted to develop a new language. I want a language that can do:
  • Static typing (required for performance reasons) but support for type inference
  • Have a rich type system that allows garbage collected object, native pointers and possibly other memory management approaches.
    There are other nice possible types that could be supported, e.g. javascript like prototyped objects.
  • Flexibility: For example sometimes I might want to control the exact layout of my structs like in C++
  • Meta-programming to the extent that it is possible to integrate domain specific languages like SQL in a nice way. This basically means that I want support for clean macros like in lisp and full compile-time reflection.
  • Easily extensible with minimal core features: Syntax and type system should be extensible. A for-each loop could be implemented as a macro. Even classes could be implemented on top of structs
    when the language supports full compile-time reflection. Garbage collected types could be build on top of non-garbage collected ones. Runtime reflection could be implemented using compile-time reflection.
  • Possible option: Support for constraints that can be checked during compile type to formally verify correctness. This could possibly be part of the type system, for example you could have a "integer between 5 and 7" type that can be converted to "integer" but not to "integer between 6 and 7" unless the compiler can prove that the value will never equal 5. You would need statements to tell the compiler "you can safely assume that variable a has type 'int between 5 and 6'" and "do a runtime check to ensure this condition" in order to help the compiler.
The reason I did not tackle this project yet is that I was not able to make up a nice type-system and a syntax that allows for full compile-time reflection and clean macros. A lisp-like syntax would be possible and implementing clean macros would become easy but I do not like lisp syntax that much. Clean macros without lisp syntax could become very hard. Another question is what types should be supported natively and what types should be derived from native types using meta-programming?

If someone had any great ideas on the syntax and type-system I could probably write a small compiler (I have some experience with compilers) but I could not make up a satisfying language definition yet.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sat Jul 21, 2012 2:20 am UTC

Bubbles McCoy wrote:Anyway, not trying to be mean here, but I sense a general theme in your posts that you care far more about how the code makes you feel rather than the quality and maintainability of your product.

Well, yes and no. Yes in that I care very much how my code makes me feel and will tend to code in such a way that I like what I've written, but no in that I tend to like high-quality, maintainable code. Non-GC code isn't necessarily lower quality and isn't really necessarily less maintainable. It is a bit more work, but it's not like that doesn't come with its advantages. A lot of the reason code makes me feel good is because of the advantages it offers: I don't like unmanaged code arbitrarily, I like it because it offers control. My point isn't that I'm okay with it despite the fact that it's worse, it's that I'm okay with it despite the fact that it isn't objectively better.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
MoD
Posts: 107
Joined: Sun Dec 30, 2007 4:55 pm UTC

Re: Coding: Fleeting Thoughts

Postby MoD » Sat Jul 21, 2012 2:35 am UTC

korona wrote:
Yakk wrote:Yes.

What I want is a C/C++ level language with the ability to do compile time introspection of class type structures, and metaclasses of classes with access to that.

So I can say "GarbageCollected class foo", which causes compile-time parsing of the methods/variables of foo. It can, for example, check that each data member of foo is also GarbageCollected. Similarly, when the foo type is used (not the instances, the type), it can validate the context (so raw pointers to foo aren't allowed). In effect, I want to be able to in-language have enough power to create a Java/C# esque "managed" typesystem.

Similarly, I want to be able to say "Reflecting class foo", and have it make the members of foo Reflect. Or "Property class foo", and when I create Property members their access has implicit access to the this pointer (in effect, I modify both code in foo, and code that uses foo). But I want this to happen with zero run-time cost, a modest compile-time cost, and with a system strong enough to do these kind of things in the language without it being a bunch of hard-coded keywords that only do this (because I don't know what else could be done this way..).

Then I can have a GC GUI low-performance reflectable system embedded adjacent to my high-performance audio filtering, without having to deal with crossing a language barrier half way.


That is exactly the reason why always wanted to develop a new language. I want a language that can do:
  • Static typing (required for performance reasons) but support for type inference
  • Have a rich type system that allows garbage collected object, native pointers and possibly other memory management approaches.
    There are other nice possible types that could be supported, e.g. javascript like prototyped objects.
  • Flexibility: For example sometimes I might want to control the exact layout of my structs like in C++
  • Meta-programming to the extent that it is possible to integrate domain specific languages like SQL in a nice way. This basically means that I want support for clean macros like in lisp and full compile-time reflection.
  • Easily extensible with minimal core features: Syntax and type system should be extensible. A for-each loop could be implemented as a macro. Even classes could be implemented on top of structs
    when the language supports full compile-time reflection. Garbage collected types could be build on top of non-garbage collected ones. Runtime reflection could be implemented using compile-time reflection.
  • Possible option: Support for constraints that can be checked during compile type to formally verify correctness. This could possibly be part of the type system, for example you could have a "integer between 5 and 7" type that can be converted to "integer" but not to "integer between 6 and 7" unless the compiler can prove that the value will never equal 5. You would need statements to tell the compiler "you can safely assume that variable a has type 'int between 5 and 6'" and "do a runtime check to ensure this condition" in order to help the compiler.
The reason I did not tackle this project yet is that I was not able to make up a nice type-system and a syntax that allows for full compile-time reflection and clean macros. A lisp-like syntax would be possible and implementing clean macros would become easy but I do not like lisp syntax that much. Clean macros without lisp syntax could become very hard. Another question is what types should be supported natively and what types should be derived from native types using meta-programming?

If someone had any great ideas on the syntax and type-system I could probably write a small compiler (I have some experience with compilers) but I could not make up a satisfying language definition yet.


This is eerily similar to what I've been working on (in strictly design stages, 0 LOC written) for a while. I lack a formal background in language design so I've been reading loads of papers and trying to understand them, but the gist of what I've been thinking about is as follows:

  • The language is roughly C-like to allow working at a low level. If anything, it's slightly lower-level than C because it allows programmers some level of compile-time information about the target processor. See next bullet point.
  • Code generation is a key facet of the language. There are facilities for specifying code to be run at compile time (to produce other code to compile, or to verify code present prior to compilation, or some combination of these), and for specifying the modification of code at runtime. In the same way C has const and non-const data variables, there will be const and non-const functions and both function pointers and direct function variables (e.g. for a function whose machine code is on the stack).
  • To make it possible to portably interact with these, there is a machine-independent representation of source code (more properly, of the AST) that is included in compiled binaries along with the functions' machine code. This is used to manipulate the function itself, and the interface is akin to that of a struct in C, with a couple notable differences. These are that the length (in number of fields), the type of each field, and the index of each field is available, so you can index into a heterogenous struct representing the code and modify it. (I'm thinking something like foo{3} for syntax but that's just a thought and might make parsing ugly if you have C-style blocks.)
  • To assist in abstracting away the process of code generation and validation, and ensure it is done correctly, there's a system of 'attributes' on types. Much like const in C, you can declare each part of a function's type with a given attribute (e.g. const char* const being a constant pointer to a constant integer). That attribute will cause a compile-time function to run on the AST of the function, and perform a check (or optionally a change, as would be necessary for e.g. garbage collection) to the function such that it returns the required type.

Issues:
  • Is this a wannabe-lisp?

    I don't think it is, considering the design of the part of the language people will be using is substantially different, and instead of lists of homoiconic entities as the method of introspection, there are array-like structs of heterogenous entities. Performance will be C-like because it's only [perhaps self-modifying, depending on the program text] machine code that ultimately gets output.

  • How portable would it be?

    The language runtime/compiler (the runtime must contain at least the backend of the compiler if it's to perform modification of functions at runtime) has to manage at least getting memory from the OS and setting permissions on it, parsing the binary representation of functions' ASTs stored in the binary, and generating output code. That doesn't seem too complex or system-dependent.

I also don't know exactly how the typesystem would work. In my head it's similar to C, with perhaps a bit of generality added and some strictness.

Some relevant links+papers+quotes:
http://lists.warhead.org.uk/pipermail/i ... 00130.html
http://dl.acm.org/citation.cfm?id=1251229.1251230
<MaleKitten> Types are propositions, and programs are proofs. If your program satisfies the type, then it is a proof of the proposition.
<rpearl> That is true, but the proposition is one that is encoded in the type. There are many derivations of certain proofs. Consider:
map :: (a -> b) -> [a] -> [b]
And [a] is shorthand for a recursive type: \forall a . \mu l . (a * l) + ().
And the translation of types to propositions means that the sum type -- either I have () or I have a list, translates to a logical connective of "or". The product type -- I have an a and the rest of the list -- translates to "and" .
The proposition is therefore that, for all a, for all b, if a implies b, then true or (a and list of a) implies true or (b and list of b).
There are several proofs of this proposition, and some of them are useless.
The standard one:
map f [] = []
map f (x:xs) = f x : map f xs
Is what we want.
There's another one, though:
map f [] = []
map f (x:xs) = [f x]
And many others that aren't so useful as the standard one.
Anyway, this has been a long and roundabout way of saying that, while a good type system give you a lot of nice static properties about a program, you simply cannot ignore dynamic semantics and rely on the types. Well-typed programs are correct in that they will not reach a state where evaluation is undefined / get stuck ("I'm supposed to do what? These are Strings, not Ints!"), but they do not imply correctness of the program, even though they do provide a proof of a proposition!
Spoiler:
L wrote:I wanna be past-MoD when I grow up
and then I guess present-MoD a little bit after that
and then future-MoD for the rest of my life!
@-={-,
}

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sat Jul 21, 2012 4:36 am UTC

God damn, I seriously need to get into functional programming. I need more motivation :|
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Sat Jul 21, 2012 7:02 am UTC

MoD wrote:tl;dq

It looks less like a wanna-be Lisp than a wanna-be Java. Granted Java trades out compile time information for runtime information in all cases, but meh. The difference is transparent so it's purely academic (except when things change at runtime, which is actually pretty important.) You might want to check out using Scala. While not "low-level" in any sense, because it can interact with Java which in turn interacts with native code (through a terrible, ugly, and slow native-code barrier) you can get most of what you want.

As for specific features you're looking for, I would recommend coming up with examples where it would be useful and where modern methods are insufficient for the task. In particular, why should functions be mutable?

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sat Jul 21, 2012 8:47 am UTC

By the way, is functional programming actually more divorced from how a computer actually works, or do I just irrationally feel that way? It seems like an S-expression doesn't map as easily to specific processor instructions as a C expression might.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Pixyn25
Posts: 10
Joined: Thu Feb 02, 2012 6:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Pixyn25 » Sat Jul 21, 2012 9:44 am UTC

sourmìlk wrote:It seems like an S-expression doesn't map as easily to specific processor instructions as a C expression might.

Some S-expressions can map onto bytecode for a stack machine.
e.g.

Code: Select all

 The (kind of) Scheme code (begin (display (+ (nextInteger) 7)) (newline)) maps to
<bytecode>
call nextInteger;
push 7;
call + (passing 2 args);
call display (passing 1 arg); <remove result from display>;
call newline;

But then it gets slightly tougher once we try to translate lambda expressions, closures, etc. I imagine you would have to store the lambdas somewhere as data (which is what S-expressions are anyway)

sourmìlk wrote:By the way, is functional programming actually more divorced from how a computer actually works, or do I just irrationally feel that way?

In what way is it divorced from the computer?

Code: Select all

town.addChangeListener(new ChangeListener(){
   public void stateChanged(ChangeEvent e){
      if(e instanceof NewPersonInTown) throw new WelcomeParty();
   }
}

Take off your shipping goggles and put on the friendshipping goggles instead.

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: Coding: Fleeting Thoughts

Postby Jplus » Sat Jul 21, 2012 9:57 am UTC

Ben-oni wrote:
MoD wrote:tl;dq

It looks less like a wanna-be Lisp than a wanna-be Java.

Funny. It reminded me most of Forth, even though the language described isn't stack-oriented. Probably because of the intermingling of compiletime and runtime.

sourmìlk wrote:By the way, is functional programming actually more divorced from how a computer actually works, or do I just irrationally feel that way? It seems like an S-expression doesn't map as easily to specific processor instructions as a C expression might.

I would say declarative languages (including functional ones) are slightly more distant from actual computer mechanics simply because computer hardware (and the entire physical world, for that matter) works imperative, where things have mutable state and order matters: "in order to achieve X first do this, then do that..." instead of "X is defined such-and-such". I believe this is one of the reasons why people tend to prefer one style over the other; it depends on whether you're a Platonist or an Aristotelian. Of course, Platonists will maintain that "actual computer mechanics" is just a shadow of what's really actually happening. ;)

I don't think the (apparent) similarity between language constructs and processor instructions is any relevant, though. In most languages most of the similarity is an illusion anyway. Even in C!

Come to think of it, Forth might be "closer to the metal" than C. Isn't that ironic?
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Sat Jul 21, 2012 12:44 pm UTC

I have no idea anymore wrote:tl;dq

You should check out Ur. It has static typing, extensible records (which are a more disciplined form of prototypes), and it checks for invalid SQL, dead links, and more that I can't remember. Details on how the type system works (technically, the kind system) is available here.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sun Jul 22, 2012 6:18 am UTC

Pixyn25 wrote:In what way is it divorced from the computer?

It seems like (and this could be wrong) that LISP or Haskell or other functional languages describe behaviours in a much more abstract way than imperative languages do. If I use a functional language to map a function to every element of a list, that's only one statement (which can be practical) in a functional language, but in an imperative statement it might have to more explicitly list the steps taken to do that. Again, I could be wrong here. This is sort of my intuitive understanding of language paradigms, I don't have data to support it. It is quite possible that I'm just more used to imperative languages and so I have an easier time visualizing how they map to discrete processor operations.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Sun Jul 22, 2012 9:45 am UTC

sourmìlk wrote:
Pixyn25 wrote:In what way is it divorced from the computer?

It seems like (and this could be wrong) that LISP or Haskell or other functional languages describe behaviours in a much more abstract way than imperative languages do. If I use a functional language to map a function to every element of a list, that's only one statement (which can be practical) in a functional language, but in an imperative statement it might have to more explicitly list the steps taken to do that. Again, I could be wrong here. This is sort of my intuitive understanding of language paradigms, I don't have data to support it. It is quite possible that I'm just more used to imperative languages and so I have an easier time visualizing how they map to discrete processor operations.

As far as Haskell goes, translating something even as trivial as map to bytecode is tricky, but mostly because of the evaluation: it's lazy, so it doesn't translate to similar imperative code. Consider a circularly linked list and apply map to it. In an imperative language, a naive implementation could loop forever unless it knows the list is circular and treats it as a special case. In Haskell, no problem. "map f $ cycle xs" is the same as "cycle $ map f xs" (note that "cycle" transforms a list into a circular list.) So, translating the code isn't as easy as it might seem.

However, with Lisp it's not so bad. A strict map is easy to follow, and implements as expected. In fact, strict, pure, functional code is easier than you think to translate to assembler. All memory is reference counted, because circular dependencies can never occur; functions are passed as a pointer to the function and a pointer to its lexically bound variables. Lisp itself isn't as high level as you make it sound: and since it's been around forever, this should make sense. It's merely the language features it offers which allow programmers to work at a higher level that make it seem like "not a low level language".

None of this should be surprising: what tools do programmers use the most? Well, it might differ, but I find that compilers and interpreters are way up there. Now, if these tools are useful to programmers, why shouldn't they have access to them all the time? That's the Lisp philosophy: programmers already use code as the data inputted into compilers, so be honest about it and give them the flexibility to do that whenever they want. After that, the only thing that makes Lisp functional is the inline functions (lambda-expressions) that can be passed and called whenever, wherever. C++0x might require careful observance of scoping rules (make sure the references you bind to will still exist by the time you call the function), but it does about the same thing.

So Lisp isn't really any higher level than C++; after all, macros are no more complex than the (turing complete) template system. Did you know you can build a Lisp entirely out of C++ templates?

Since declarative programming is the holy grail of all programmers working in all languages, the fact that you can do such things in even C++ shouldn't be surprising. So there really isn't much difference between Lisp and C++, except for the built-in garbage collector (and even that depends on your flavor).

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sun Jul 22, 2012 12:23 pm UTC

Methinks I need to get into LISP. And then Haskell, but for a reason I can't quite put my finger on, Haskell scares me a bit. Do you all recommend CLisp or Scheme? Did I just ask a stupid or inflammatory question?
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Sun Jul 22, 2012 3:14 pm UTC

I'm a Common Lisper, but Scheme does have some things to recommend it. It also has some things to dis-recommend it, like their weirdness about macros.

I'd recommend learning Clojure if you started learning a Lisp today, though.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Sun Jul 22, 2012 3:20 pm UTC

sourmìlk wrote:Methinks I need to get into LISP. And then Haskell, but for a reason I can't quite put my finger on, Haskell scares me a bit. Do you all recommend CLisp or Scheme? Did I just ask a stupid or inflammatory question?

See here for my comments.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sun Jul 22, 2012 3:25 pm UTC

Clojure is compatible with Java libraries? Holy shit, I'm using clojure. With Java libraries available to me I can think of plenty of things I'd like to do.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Sun Jul 22, 2012 3:35 pm UTC

sourmìlk wrote:Clojure is compatible with Java libraries? Holy shit, I'm using clojure. With Java libraries available to me I can think of plenty of things I'd like to do.

Just in case my other post wasn't clear, other Lisps will usually give you a C FFI, so you can also do plenty with those. :-) But if you want to use a library that someone hasn't already written bindings for (e.g. through SWIG), it's not a clean boundary and you've got some work to do. (As opposed to Clojure, which Just Works.)

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Sun Jul 22, 2012 3:40 pm UTC

Ben-oni wrote:As far as Haskell goes, translating something even as trivial as map to bytecode is tricky, but mostly because of the evaluation: it's lazy, so it doesn't translate to similar imperative code. Consider a circularly linked list and apply map to it. In an imperative language, a naive implementation could loop forever unless it knows the list is circular and treats it as a special case. In Haskell, no problem. "map f $ cycle xs" is the same as "cycle $ map f xs" (note that "cycle" transforms a list into a circular list.) So, translating the code isn't as easy as it might seem.
Exposure to Python's generators can help understand what kind of imperative code could substitute here.

A python generator is a function with two return states -- it either returns an element, or an error (via throw, but that isn't important) that it is out of elements. You query it for each element in turn, and it produces the element you asked for "on demand". It could be backed by a list that actually has the elements, or it could not be -- it doesn't matter to the user.

So, instead of thinking of map taking a list and returning a list, think of it as taking a generator for a list and returning a generator for a list. Naturally, it isn't quite that simple, but it should give you an idea that isn't "and then magic happens". :)
Did you know you can build a Lisp entirely out of C++ templates?
Note that this isn't true in practice, because of the recursion limit that both the standard allows and compilers enforce (by not handling deeper recursion). :)

However, every C++ programmer should at least write the compile-time Fibonacci.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sun Jul 29, 2012 6:42 am UTC

A lambda function in a std::map<int, std::function<stuff>> segmentation faults.

dat stack trace

Code: Select all

?? ()
std::function<void (CharacterEventType, Character*)>::function(std::function<void (CharacterEventType, Character*)> const&) (this=0x7fffffffdd08, __x=@0x7ffff7f4a130: {<std::_Maybe_unary_or_binary_function<void, CharacterEventType, Character*>> = {<std::binary_function<CharacterEventType, Character*, void>> = {<No data fields>}, <No data fields>}, <std::_Function_base> = {static _M_max_size = 16, static _M_max_align = 8, _M_functor = {_M_unused = {_M_object = 0x2, _M_const_object = 0x2, _M_function_pointer = 0x2, _M_member_pointer = (void (std::_Undefined_class::*)(std::_Undefined_class * const)) 0x2, this adjustment 270582939711}, _M_pod_data = \"\\002\\000\\000\\000\\000\\000\\000\\000?\\000\\000\\000?\\000\\000\"}, _M_manager = 0x623150}, _M_invoker = 0})
std::pair<int, std::function<void (CharacterEventType, Character*)> >::pair<int const, std::function<void (CharacterEventType, Character*)>, void>(std::pair<int const, std::function<void (CharacterEventType, Character*)> > const&) (this=0x7fffffffdd00, __p=@0x7ffff7f4a128: {first = 1, second = {<std::_Maybe_unary_or_binary_function<void, CharacterEventType, Character*>> = {<std::binary_function<CharacterEventType, Character*, void>> = {<No data fields>}, <No data fields>}, <std::_Function_base> = {static _M_max_size = 16, static _M_max_align = 8, _M_functor = {_M_unused = {_M_object = 0x2, _M_const_object = 0x2, _M_function_pointer = 0x2, _M_member_pointer = (void (std::_Undefined_class::*)(std::_Undefined_class * const)) 0x2, this adjustment 270582939711}, _M_pod_data = \"\\002\\000\\000\\000\\000\\000\\000\\000?\\000\\000\\000?\\000\\000\"}, _M_manager = 0x623150}, _M_invoker = 0}})


--edit--

Turns out, if you cause a lambda function to fall out of scope from within the lambda function while it's running and accessing its data, bad things occasionally happen.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Sun Jul 29, 2012 8:05 am UTC

I've been learning Haskell for roughly 1 week.

Is this a sensible way to generate an alternating sequence of integers, or is there a standard function to replace my zipWith ($) ?

Code: Select all

zipWith ($) (cycle [id,negate]) [1..]

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Sun Jul 29, 2012 8:34 am UTC

PM 2Ring wrote:I've been learning Haskell for roughly 1 week.

Is this a sensible way to generate an alternating sequence of integers, or is there a standard function to replace my zipWith ($) ?

Code: Select all

zipWith ($) (cycle [id,negate]) [1..]

One of the beauties of Haskell is that it mirrors mathematical notations quite elegantly. While your method definitely works, I don't think it's at all sensible, because it obscures the underlying math. Usually we write ak = {(-1)n+1n} for n in N, which translates naturally to

Code: Select all

map (\n -> (-1)^(n+1) * n) [1..]


Or, more clearly:

Code: Select all

map (\n -> if even n then -n else n) [1..]


If what you're really looking at is a way to apply a function to every other element...

Code: Select all

unfoldr (\(fs, n) -> Just (head fs n, (tail fs, n+1))) (cycle [id, negate], 1)
...yes, we can always find more complicated ways of doing simple things. But then there's simple:

Code: Select all

zipWith (*) (cycle [1, -1]) [1..]


This last one also appears to perform fastest, though I only did a simple test without any optimizations on.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sun Jul 29, 2012 10:06 am UTC

Ben-oni wrote:One of the beauties of Haskell...

I feel like I should make fun of you for having a love affair with functional programming, but frankly I have similar reactions to (of all things) C++. (I secretly don't think it's overrated, and that it's quite good and powerful).
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sun Jul 29, 2012 10:27 am UTC

sourmìlk wrote:
Ben-oni wrote:One of the beauties of Haskell...

I feel like I should make fun of you for having a love affair with functional programming, but frankly I have similar reactions to (of all things) C++. (I secretly don't think it's overrated, and that it's quite good and powerful).


To be fair, C++ did get hella sexy with C++11.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sun Jul 29, 2012 10:40 am UTC

It is largely C++11 that I like. I mean, C++ was already pretty cool with templates, but C++11's additions are awesome. Type traits and variadic templates alone add so much, and that's not even counting some of the things that should have been there all along, like final and override keywords etc. I can't wait until more compilers support more features.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Sun Jul 29, 2012 12:28 pm UTC

PM 2Ring wrote:I've been learning Haskell for roughly 1 week.

Is this a sensible way to generate an alternating sequence of integers, or is there a standard function to replace my zipWith ($) ?

Code: Select all

zipWith ($) (cycle [id,negate]) [1..]


Incidentally, if you want it to have all the integers and not just alternating ones, you can do it with

Code: Select all

0 : ([id, negate] <*> [1..])
-- 0, 1, -1, 2, -2, ...
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Sun Jul 29, 2012 7:43 pm UTC

sourmìlk wrote:I feel like I should make fun of you for having a love affair with functional programming, but frankly I have similar reactions to (of all things) C++. (I secretly don't think it's overrated, and that it's quite good and powerful).

You don't know what you're talking about, and the continual insults are starting to try my patience. Please stop.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sun Jul 29, 2012 9:51 pm UTC

Ben-oni wrote:
sourmìlk wrote:I feel like I should make fun of you for having a love affair with functional programming, but frankly I have similar reactions to (of all things) C++. (I secretly don't think it's overrated, and that it's quite good and powerful).

You don't know what you're talking about, and the continual insults are starting to try my patience. Please stop.

???
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sun Jul 29, 2012 11:23 pm UTC

Ben-oni wrote:
sourmìlk wrote:I feel like I should make fun of you for having a love affair with functional programming, but frankly I have similar reactions to (of all things) C++. (I secretly don't think it's overrated, and that it's quite good and powerful).

You don't know what you're talking about, and the continual insults are starting to try my patience. Please stop.


...?

I have not once insulted you, including there.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Coding: Fleeting Thoughts

Postby hotaru » Mon Jul 30, 2012 3:37 am UTC

headprogrammingczar wrote:Incidentally, if you want it to have all the integers and not just alternating ones, you can do it with

Code: Select all

0 : ([id, negate] <*> [1..])
-- 0, 1, -1, 2, -2, ...

really?

Code: Select all

Prelude Control.Applicative> take 10 $ 0 : ([id, negate] <*> [1..])
[0,1,2,3,4,5,6,7,8,9]


now this, on the other hand, actually does what you were trying to do, but is much less pretty:

Code: Select all

0 : (flip ($) <$> [1..] <*> [id, negate])

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Mon Jul 30, 2012 4:43 am UTC

Thanks for the replies, people. I'm sure I'll have more questions soon. I'm still trying to get my head around programming without iterative loops or assignment. It's very hard to break the imperative style thought patterns of several decades...

Ben-oni wrote:
PM 2Ring wrote:I've been learning Haskell for roughly 1 week.

Is this a sensible way to generate an alternating sequence of integers, or is there a standard function to replace my zipWith ($) ?

Code: Select all

zipWith ($) (cycle [id,negate]) [1..]

One of the beauties of Haskell is that it mirrors mathematical notations quite elegantly. While your method definitely works, I don't think it's at all sensible, because it obscures the underlying math. Usually we write ak = {(-1)n+1n} for n in N, which translates naturally to

Code: Select all

map (\n -> (-1)^(n+1) * n) [1..]


Or, more clearly:

Code: Select all

map (\n -> if even n then -n else n) [1..]


If what you're really looking at is a way to apply a function to every other element...

Code: Select all

unfoldr (\(fs, n) -> Just (head fs n, (tail fs, n+1))) (cycle [id, negate], 1)
...yes, we can always find more complicated ways of doing simple things. But then there's simple:

Code: Select all

zipWith (*) (cycle [1, -1]) [1..]


This last one also appears to perform fastest, though I only did a simple test without any optimizations on.


Well, I did try the zipWith (*) approach, but I figured that zipWith ($) was slightly more elegant and perhaps more efficient. Believe it or not, my first thought was to use something like (\n -> (-1)^(n+1) * n), but I felt that a method that didn't use exponentiation ought to be more efficient, although I guess that the optimisation genie could take care of that.

I guess that map (\n -> if even n then -n else n) [1..] is probably the simplest & clearest approach in this case, OTOH, it would require modificaton if I wanted to create a different alternating sequence, eg [1,-3,5,-7,...].

I find

Code: Select all

unfoldr (\(fs, n) -> Just (head fs n, (tail fs, n+1))) (cycle [id, negate], 1)

a bit baffling at this stage. I suppose I need to sit down with pencil & paper and work out what's going on there. Why does it use Just?

User avatar
chridd
Has a vermicelli title
Posts: 846
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Mon Jul 30, 2012 5:05 am UTC

PM 2Ring wrote:Why does it use Just?
unfoldr is of type (b -> Maybe (a, b)) -> b -> [a]
The function can return Nothing to end the list; since this is an infinite list, that feature happens not to be used.

Edit: Example:

Code: Select all

Prelude Data.List> unfoldr (\(fs, n) -> if n >= 10 then Nothing else Just (head fs n, (tail fs, n+1))) (cycle [id, negate], 1)
[1,-2,3,-4,5,-6,7,-8,9]
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Mon Jul 30, 2012 5:22 am UTC

Ah. Thanks, chridd. I have read about the fold family, but I'd forgotten that they return a Maybe. I'm currently reading the later chapters of "Learn You a Haskell For Great Good", but it's all getting a bit jumbled up in my head. :)

User avatar
chridd
Has a vermicelli title
Posts: 846
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Mon Jul 30, 2012 7:07 am UTC

PM 2Ring wrote:Ah. Thanks, chridd. I have read about the fold family, but I'd forgotten that they return a Maybe. I'm currently reading the later chapters of "Learn You a Haskell For Great Good", but it's all getting a bit jumbled up in my head. :)
In case I wasn't clear: The fold functions don't return Maybe (unless that happens to be the type you choose for it to return), but the function you pass to unfoldr does.
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Mon Jul 30, 2012 7:48 am UTC

chridd wrote:
PM 2Ring wrote:Ah. Thanks, chridd. I have read about the fold family, but I'd forgotten that they return a Maybe. I'm currently reading the later chapters of "Learn You a Haskell For Great Good", but it's all getting a bit jumbled up in my head. :)
In case I wasn't clear: The fold functions don't return Maybe (unless that happens to be the type you choose for it to return), but the function you pass to unfoldr does.


Oops! You were clear; I just wasn't reading properly. :)

I'm currently writing a Haskell program to calculate e to arbitrary precision. I've just got it working correctly, but I'm sure it's rather inefficient. In an imperative language, I'd print each block of digits as I generate them, but I don't know how to do that sort of thing in Haskell, so I'm just collecting all the digits into a list & printing them out at the end.

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: Coding: Fleeting Thoughts

Postby Jplus » Mon Jul 30, 2012 9:46 am UTC

PM 2Ring wrote:I'm currently writing a Haskell program to calculate e to arbitrary precision. I've just got it working correctly, but I'm sure it's rather inefficient. In an imperative language, I'd print each block of digits as I generate them, but I don't know how to do that sort of thing in Haskell, so I'm just collecting all the digits into a list & printing them out at the end.

Depending on how exactly you're writing your program, the latter might very well amount to the former.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Mon Jul 30, 2012 9:55 am UTC

PM 2Ring wrote:I'm currently writing a Haskell program to calculate e to arbitrary precision. I've just got it working correctly, but I'm sure it's rather inefficient. In an imperative language, I'd print each block of digits as I generate them, but I don't know how to do that sort of thing in Haskell, so I'm just collecting all the digits into a list & printing them out at the end.

The easy way, I think, would be to calculate e as a Rational and then print the Rational. I can get the first thousand digits in a few seconds, but it's hardly efficient (10000 decimal places takes several minutes). But now I have more e than I'll ever need.

If you have an incremental solution, would mind sharing your code?

Spoiler:

Code: Select all

e =
2.718281828459045235360287471352662497757247093699959574966967627724
07663035354759457138217852516642742746639193200305992181741359662904
35729003342952605956307381323286279434907632338298807531952510190115
73834187930702154089149934884167509244761460668082264800168477411853
74234544243710753907774499206955170276183860626133138458300075204493
38265602976067371132007093287091274437470472306969772093101416928368
19025515108657463772111252389784425056953696770785449969967946864454
90598793163688923009879312773617821542499922957635148220826989519366
80331825288693984964651058209392398294887933203625094431173012381970
68416140397019837679320683282376464804295311802328782509819455815301
75671736133206981125099618188159304169035159888851934580727386673858
94228792284998920868058257492796104841984443634632449684875602336248
27041978623209002160990235304369941849146314093431738143640546253152
09618369088870701676839642437814059271456354906130310720851038375051
01157477041718986106873969655212671546889570350354021234078498193343
21068170121005627880235193033224745015853904730419957777093503660416
99732972508868769664035557071622684471625607988265178713419512466520
10305921236677194325278675398558944896970964097545918569563802363701
62112047742722836489613422516445078182442352948636372141740238893441
24796357437026375529444833799801612549227850925778256209262264832627
79333865664816277251640191059004916449982893150566047258027786318641
55195653244258698294695930801915298721172556347546396447910145904090
58629849679128740687050489585867174798546677575732056812884592054133
40539220001137863009455606881667400169842055804033637953764520304024
32256613527836951177883863874439662532249850654995886234281899707733
27617178392803494650143455889707194258639877275471096295374152111513
68350627526023264847287039207643100595841166120545297030236472549296
66938115137322753645098889031360205724817658511806303644281231496550
70475102544650117272115551948668508003685322818315219600373562527944
95158284188294787610852639813955990067376482922443752871846245780361
92981971399147564488262603903381441823262515097482798777996437308997
03888677822713836057729788241256119071766394650706330452795466185509
66661856647097113444740160704626215680717481877844371436988218559670
95910259686200235371858874856965220005031173439207321139080329363447
97273559552773490717837934216370120500545132638354400018632399149070
54797780566978533580489669062951194324730995876552368128590413832411
60722602998330535370876138939639177957454016137223618789365260538155
84158718692553860616477983402543512843961294603529133259427949043372
99085731580290958631382683291477116396337092400316894586360606458459
25126994655724839186564209752685082307544254599376917041977780085362
73094171016343490769642372229435236612557250881477922315197477806056
96725380171807763603462459278778465850656050780844211529697521890874
01966090665180351650179250461950136658543663271254963990854914420001
45747608193022120660243300964127048943903971771951806990869986066365
83232278709376502260149291011517177635944602023249300280401867723910
28809786660565118326004368850881715723866984224220102495055188169480
32210025154264946398128736776589276881635983124778865201411741109136
01164995076629077943646005851941998560162647907615321038727557126992
51827568798930276176114616254935649590379804583818232336861201624373
65698467037858533052758333379399075216606923805336988795651372855938
83499894707416181550125397064648171946708348197214488898790676503795
90366967249499254527903372963616265897603949857674139735944102374432
97093554779826296145914429364514286171585873397467918975712119561873
85783644758448423555581050025611492391518893099463428413936080383091
66281881150371528496705974162562823609216807515017772538740256425347
08790891372917228286115159156837252416307722544063378759310598267609
44203261924285317018781772960235413060672136046000389661093647095141
41718577701418060644363681546444005331608778314317444081194942297559
93140118886833148328027065538330046932901157441475631399972217038046
17092894579096271662260740718749975359212756084414737823303270330168
23719364800217328573493594756433412994302485023573221459784328264142
16848787216733670106150942434569844018733128101079451272237378861260
58165668053714396127888732527373890392890506865324138062796025930387
72769778379286840932536588073398845721874602100531148335132385004782
71693762180049047955979592905916554705057775143081751126989851884087
18564026035305583737832422924185625644255022672155980274012617971928
04713960068916382866527700975276706977703643926022437284184088325184
87704726384403795301669054659374616193238403638931313643271376888410
26811219891275223056256756254701725086349765367288605966752740868627
40791285657699631378975303466061666980421826772456053066077389962421
83408598820718646826232150802882863597468396543588566855037731312965
87975810501214916207656769950659715344763470320853215603674828608378
65680307306265763346977429563464371670939719306087696349532884683361
30388294310408002968738691170666661468000151211434422560238744743252
50769387077775193299942137277211258843608715834835626961661980572526
61220679754062106208064988291845439530152998209250300549825704339055
35701686531205264956148572492573862069174036952135337325316663454665
88597286659451136441370331393672118569553952108458407244323835586063
10680696492485123263269951460359603729725319836842336390463213671011
61928217111502828016044880588023820319814930963695967358327420249882
45684941273860566491352526706046234450549227581151709314921879592718
00194096886698683703730220047531433818109270803001720593553052070070
60722339994639905713115870996357773590271962850611465148375262095653
46713290025994397663114545902685898979115837093419370441155121920117
16488056694593813118384376562062784631049034629395002945834116482411
49697583260118007316994373935069662957124102732391387417549230718624
54543222039552735295240245903805744502892246886285336542213815722131
16328811205214648980518009202471939171055539011394331668151582884368
76069611025051710073927623855533862725535388309606716446623709226468
09671254061869502143176211668140097595281493907222601112681153108387
31761732323526360583817315103459573653822353499293582283685100781088
46343499835184044517042701893819942434100905753762577675711180900881
64183319201962623416288166521374717325477727783488774366518828752156
68571950637193656539038944936642176400312152787022236646363575550356
55769488865495002708539236171055021311474137441061344455441921013361
72996285694899193369184729478580729156088510396781959429833186480756
08367955149663644896559294818785178403877332624705194505041984774201
41839477312028158868457072905440575106012852580565947030468363445926
52552137008068752009593453607316226118728173928074623094685367823106
09792159936001994623799343421068781349734695924646975250624695861690
91785739765951993929939955675427146549104568607020990126068187049841
78079173924071945996323060254707901774527513186809982284730860766536
86685551646770291133682756310722334672611370549079536583453863719623
58563126183871567741187385277229225947433737856955384562468010139057
27871016512966636764451872465653730402443684140814488732957847348490
00301947788802046032466084287535184836495919508288832320652212810419
04480472479492913422849519700226013104300624107179715027934332634079
95960531446053230488528972917659876016667811937932372453857209607582
27717848336161358261289622611812945592746276713779448758675365754486
14076119311259585126557597345730153336426307679854433857617153334623
25270572005303988289499034259566232975782488735029259166825894456894
65599265845476269452878051650172067478541788798227680653665064191097
34345288783386217261562695826544782056729877564263253215942944180399
43217000090542650763095588465895171709147607437136893319469090981904
50129030709956622662030318264936573369841955577696378762491885286568
66076005660256054457113372868402055744160308370523122425872234388541
23179481388550075689381124935386318635287083799845692619981794523364
08742959118074745341955142035172618420084550917084568236820089773945
58426792142734775608796442792027083121501564063413416171664480698154
83764491573900121217041547872591998943825364950514771379399147205219
52907939613762110723849429061635760459623125350606853765142311534966
56837151166042207963944666211632551577290709784731562782775987881364
91951257483328793771571459091064841642678309949723674420175862269402
15940792448054125536043131799269673915754241929660731239376354213923
06178767539587114361040894099660894714183406983629936753626215452472
98464213752891079884381306095552622720837518629837066787224430195793
79378607210725427728907173285487437435578196651171661833088112912024
52040486822000723440350254482028342541878846536025915064452716577000
44521097735585897622655484941621714989532383421600114062950718490427
78925855274303522139683567901807640604213830730877446017084268827226
11771808426643336517800021719034492342642662922614560043373838683355
55343453004264818473989215627086095650629340405264943244261445665921
29122564889356965500915430642613425266847259491431423939884543248632
74618428466559853323122104662598901417121034460842716166190012571958
70793217569698544013397622096749454185407118446433946990162698351607
84892451405894094639526780735457970030705116368251948770118976400282
76484141605872061841852971891540196882532893091496653457535714273184
82016384644832499037886069008072709327673127581966563941148961716832
98045513972950668760474091542042842999354102582911350224169076943166
85742425225090269390348148564513030699251995904363840284292674125734
22447765584177886171737265462085498294498946787350929581652632072258
99236876845701782303809656788311228930580914057261086588484587310165
81511675333276748870148291674197015125597825727074064318086014281490
24146780472327597684269633935773542930186739439716388611764209004068
66339885684168100387238921448317607011668450388721236436704331409115
57332801829779887365909166596124020217785588548761761619893707943800
56663364884365089144805571039765214696027662583599051987042300179465

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Mon Jul 30, 2012 12:24 pm UTC

PM 2Ring wrote:In an imperative language, I'd print each block of digits as I generate them, but I don't know how to do that sort of thing in Haskell, so I'm just collecting all the digits into a list & printing them out at the end.

Bear in mind that Haskell is lazy... so if your code is generating the digits (or blocks of digits) in order, and whatever you're passing them to on the IO side is looping over the list and printing them as they come (on both counts: probably) then laziness will do that for you.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Mon Jul 30, 2012 12:59 pm UTC

I've been writing a PHP library for developers working on common websites. Basically, it's a proper framework built from what I've seen countless people re-engineer every time they make a new website. The make a user system, a blog system, a way to store files, a contact form system... And I just realized how insane this feature creep is, even when I'm building with developer flexibility in mind. I'm really seeing why developers are burned out trying to hand-code everything from scratch, and libraries/frameworks can take too much of a learning curve.
In Minecraft, I use the username Rirez.

User avatar
tastelikecoke
Posts: 1208
Joined: Mon Feb 01, 2010 7:58 am UTC
Location: Antipode of Brazil
Contact:

Re: Coding: Fleeting Thoughts

Postby tastelikecoke » Mon Jul 30, 2012 2:05 pm UTC

Today I tried to write a simple, in-place, quick sort in C. 24 hours later I realized that quick sort is an unstable sort, so I returned to a simple insertion sort.

I should really code in C more often. Python's spoonfed me for a long time.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Mon Jul 30, 2012 2:26 pm UTC

Writing a simple quick sort in C is easy. First, write a wrapper around a C++ source file, and call std::stable_sort( ptr, ptr+length ).
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
tastelikecoke
Posts: 1208
Joined: Mon Feb 01, 2010 7:58 am UTC
Location: Antipode of Brazil
Contact:

Re: Coding: Fleeting Thoughts

Postby tastelikecoke » Mon Jul 30, 2012 4:51 pm UTC

I got lost there. Do you mean importing C++ code into C or is that supposed to be C source file? I don't know if I can use the standard library that liberally on our assignment.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests