Jump to content

Daniel0

Staff Alumni
  • Posts

    11,885
  • Joined

  • Last visited

About Daniel0

Profile Information

  • Gender
    Male

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Daniel0's Achievements

Advanced Member

Advanced Member (4/5)

2

Reputation

  1. It isn't random. It's pseudorandom. Given the same seed, you'll always get the same number. PRNGs generate numbers that are difficult to predict without the seed so they look random.
  2. 1+i
  3. This one for vim: https://github.com/tomasr/molokai
  4. I'm not sure it's worth adding one more example in a new language. Any Turing-complete language will be able to express the Y combinator (though some prettier than others). LOL. I know what kind of face I'd be getting if I said that to my colleagues because I named all my functions y($x) x($y). Well, if you know what the Y combinator is, then you'll understand the naming convention of $f and $g. If you don't, giving them other names wouldn't help you at all
  5. It's the Y combinator, so it's called "Y" on purpose. It computes the fixpoint in (untyped) λ-calculus. $f and $g are just functions, so they're properly named as well by standard mathematical conventions. Given to Y is a function that given a "partial" Fibonacci function returns a new function that can compute one more recursive step of the Fibonacci function, so to speak (that is, if it can compute f(n), then the new function can compute f(n+1)). The fixpoint of this function is the "full" Fibonacci function. The Y combinator allows you to express any recursive computation without using recursion.
  6. Nah... a perfect script should stick as close to λ-calculus and functional programming as possible. For instance, clearly this is the most elegant way to print the first 10 Fibonacci numbers: <?php function Y($f) { $ff = function($f) { return $f($f); }; // PHP doesn't allow function call chaining return $ff( function ($g) use ($f) { return $f(function() use ($g) { return call_user_func_array($g($g), func_get_args()); }); } ); } $fib = Y(function($fib) { return function($n) use ($fib) { return $n === 0 ? 0 : ($n === 1 ? 1 : $fib($n-1) + $fib($n-2)); }; }); function tabulate($n, $f) { $g = Y(function($a) use ($n, $f) { return function($l, $m) use ($n, $a, $f) { return $n === $m ? $l : $a(array_merge([$f($m)], $l), $m + 1); }; }); return $g([], 0); } $disp = function($f, $n) { printf("f(%d) = %d\n", $n, $f($n)); }; tabulate(10, function($n) use ($fib, $disp) { $disp($fib, $n); }); Output: f(0) = 0 f(1) = 1 f(2) = 1 f(3) = 2 f(4) = 3 f(5) = 5 f(6) = 8 f(7) = 13 f( = 21 f(9) = 34
  7. You don't need to understand the first part of my post to become a PHP programmer. I only touched the tip of the iceberg. If you find it interesting and you'd like to learn about compiler theory, I would recommend you to read a book or take a course on compilers (the latter would likely include the former as well though). I'm a computer science student, so compiler and formal language theory are some of the subjects I deal with, but you don't need to become a computer scientist to become a web developer. I don't expect you to understand all of it at this point, but there should be enough keywords for you to research should you decide to look at it further. An understanding of the HTTP protocol is pretty essential if you want to become a web developer though, even if you at first only have a rudimentary understanding of it.
  8. That's not entirely true. The following outputs hello: <?php test(); function test() { echo 'hello'; } If it was strictly line by line, top to bottom, you couldn't execute this because the test() function didn't exist yet. To understand why this is the case, you need to understand how interpreters/compilers work. The work is typically split into multiple, distinct steps. The first step is called lexical analysis, which is the process of turning a series of characters into a series of "tokens". For instance, the string echo in PHP corresponds to the T_ECHO token in PHP (the list of tokens in PHP can be found at http://php.net/tokens). The part of the program that does this is typically called a lexer or a tokenizer. The string "echo 1+1" would result in the following tokens: T_ECHO, T_WHITESPACE, T_LNUMBER, +, T_LNUMBER. The next step is syntax analysis (also called parsing), which takes a series of tokens as input and gives an "abstract syntax tree" (AST) as output. It does this using a predefined grammar that consists of a set of "productions" consisting of "terminals" and "non-terminals". Typically you would describe this grammar using a formalized notation such as (Extended) Backus-Naur Form ((E)BNF) and use a parser generator to get a parser, because hand-coding it is tedious and error prone. The above example string would result in the following tree: T_ECHO | + / \ T_LNUMBER(1) T_NUMBER(2) A simple EBNF grammar for a mathematical expression (integers only) with the common arithmetic operators and grouping with parentheses could look like this (precedence rules work from top to bottom): digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; number = digit, { digit } ; expr = number | plus | minus | div | mult | group ; group = "(", expr, ")" ; mult = expr, "*", expr ; div = expr, "/", expr ; plus = expr, "+", expr ; minus = expr, "-", expr ; Here, each line is a production, each thing in quotes is called a terminal and the others are called non-terminals. Given a set of productions, you can using a process called derivation. There exist multiple strategies for doing this, e.g. LL(1) parsing, SLR parsing, etc. After the parsing, you can have a type checker, e.g. to ensure that both operands of an arithmetic operator are guaranteed to be numeric. PHP does runtime type inference, so it doesn't have a type checker. There is no difference between a compiler and an interpreter up until this point. At this point, an interpreter will evaluate the AST by recursively traversing it. A compiler will transform the AST into a another, typically lower level, language. Again, this would be using recursive traversal. This may be something like LLVM, bytecode for JVM, assembler for some CPU architecture or something else. In theory, there is nothing preventing you from taking a PHP AST and turning it into Python source code, or English prose describing what is going on if it would be executed. When evaluating the AST, you may create a lookup table consisting of in-scope variables, functions, etc. Again, this is not entirely true. To understand why this is the case, you need to understand how the HTTP protocol works. HTTP is a stateless protocol defined in RFC 2616, which works by sending a request (with some headers) and getting a response in return (with some headers). To login, my user agent (e.g. a browser) could send a request like this: POST /login.php HTTP/1.1 Host: www.example.com User-Agent: whatever Content-Type: application/x-www-form-urlencoded ***possibly more headers*** username=Daniel&password=s3cret When the web server receives this request, it may "forward" it to PHP so it can generate an appropriate response. Assuming the credentials given are correct, you may choose to set some cookie so you can remember him (remember, HTTP is stateless and all requests are entirely separated) and you may use the Location response header to indicate to the client that it should go somewhere else when it is done. However, this doesn't stop the execution of your script. Doing this could be problematic as well. You might still have some cleaning up to do (closing connections to other places, releasing file locks, etc.), or you may have more headers you want to send. If you want the execution to stop, you'll have to explicitly do it using die()/exit() or simply make sure that there are no executable statements after. When you have done this, the web server may send something like the following request back over the same TCP socket: HTTP/1.1 200 OK Location: http://www.example.com/index.php Set-Cookie: session_id=kasdfusd89f79ads8f7g Actually, RFC 2616 says that a user agent "MUST NOT" automatically redirect as a result of a Location response header without user intervention if the request was not GET or HEAD, but most browsers seem to ignore that. At any rate, the Location is only a suggestion for the client to retrieve the new resource. The client is free to ignore it. You may verify that this is true by placing the following file in a web-accessible directory and requesting it using a browser: <?php header('Location: http://www.google.com/'); file_put_contents('new_file.txt', 'Was not here before...'); ?> Assuming the web server has write permissions in that directory, you should find a new file called new_file.txt in that directory even though your browser was redirected to Google.
  9. Actually, speed is a factor on StackOverflow. If you're late to the party, your chance of getting many upvotes is diminished.
  10. Sure, it's always good getting multiple points of views on an issue.
  11. Hmm... but majority vote isn't necessarily a measure of quality or correctness. I'm not convinced that this is the solution. Hypothetical scenario: User A asks a question, user B responds with a half-decent answer and gets 5 votes for that. Now user C provides a much better answer (let's just for the sake of argument suppose that we by some objective standard can determine that). User A returns to his post and observes that user B has 5 votes and user C has 0, indicating that the answer provided by user B is better, contrary to reality. Now what?
  12. I'm sure the person responsible will feel god's wrath and burn in hell.
  13. Can't blame him. Whether your stuff gets implemented is dependent on that there is not some idiot on the top who vetos because it's against some stupid axiom he made for that special occasion. There may be a million votes for the proposol, and none against, but that doesn't matter if someone decides that his personal vision is more important than a community decision. That certainly does happen, but if you never even attempted to submit the patches, it's guaranteed that they won't be accepted. I was basing my rant on particular encounters with certain members of the PHP Group. I can't be bothered going into details, but as a result I ultimately decided that I no longer wanted to contribute. Of course there needs to be leadership, but there is a such thing as bad leadership.
  14. Can't blame him. Whether your stuff gets implemented is dependent on that there is not some idiot on the top who vetos because it's against some stupid axiom he made for that special occasion. There may be a million votes for the proposol, and none against, but that doesn't matter if someone decides that his personal vision is more important than a community decision.
  15. Sure it is. Just ask Sony
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.