3 ms·
I have no idea how to even parse this: (or (if (and (weak? player) If it is a player or a weak player? Or this probably simple function(?): (defn- w
by aw3c2 12y ago
I have no idea how to even parse this:
(or (if (and (weak? player)
If it is a player or a weak player?
Or this probably simple function(?):
(defn- want-protection? [game]
(and (< (:protection (:player game)) 3) (> 15 (:xplvl (:player game)))))
It makes zero intuitive sense to me.
- lmm 12y agoLISP-style languages are a pain to read, but it's purely mechanical: everything's in "polish notation", (function arg1 arg2), even when function is something that we would want to think of as an operator or an accessor method. So in Scala I'd write that function as: def want-protection(game) = game.player.protection < 3 && game.player.xplvl < 15
- deleted 12y ago[deleted]
- schoen 12y agoThat first line is just the beginning of a statement that goes on until line 71. It decides whether to eat or pray if the player is very hungry. (In NetHack you can die of starvation if you don't have enough nutrition; nutrition can be obtained either by eating food[1], or in some circumstances by praying when hungry.) The second function is easier for me to understand: it says that the player wants protection if the player has less than 3 intrinsic protection and has an experience level higher than 15. (In NetHack, gods grant intrinsic protection under some circumstances, which causes one to become better armored even without wearing additional physical armor. It's basically like a kind of magical armor on one's body that's independent of any physical object.) The idea seems to be that a player with at least experience level 15 ought to have 3 or more points of protection, and if the player doesn't, the player should try to go and get them. If you're not used to reading parenthesized prefix notation, you might have a look at https://en.wikipedia.org/wiki/Polish_notation https://en.wikipedia.org/wiki/Polish_notation https://en.wikipedia.org/wiki/S-expression https://en.wikipedia.org/wiki/S-expression https://en.wikipedia.org/wiki/Lisp_%28programming_language%29 https://en.wikipedia.org/wiki/Lisp_%28programming_language%2... [1] In another example of NetHack's complexity, it's also possible in some circumstances to eat objects other than food, by turning into a monster that's able to eat the objects in question.
- Elrac 12y agoHi! Here's a few explaining words: The first one is missing a whole bunch of context. But here's a start: The first thing inside parentheses is the operator/function, the rest are arguments/operands. This one is best viewed from the inside out, so... a method ending in a question mark is most likely a predicate, i.e. a true/false test returning a boolean. So we're testing for a weak player. Outside from there is an 'and', so we're looking to combine that test with another one (further to the right, that you didn't show). Maybe checking if the player is both weak and hungry, as that may mean he'll croak soon. Every paren pair ('S-expression') in Lisp (err, Clojure) returns a value, so the "if" is not so much a control-flow construct as it's like C's ternary op: [ cond ? iftrue : iffalse ]. Its first argument will be a test, like that weak-hunger thing. Then you will have one or maybe two other arguments. The first is returned if the condition is true, the second one if false. There may not be a second argument, in which case the return value will be 'nil' or maybe 'false' - they're sorta equivalent anyway. Finally, the "or" will evaluate and return its first subexpression if it's true, or also evaluate the second one if not and return that. You can actually have as many arguments to an "or" as you like - they will be short-circuit-evaluated until one of the arguments is true, or if you hit the right paren without finding a true, you'll get a 'false' ('nil' ?). ------------------------------------ The second example is a function definition. By convention, 'want-protection?' will be the name of a predicate, i.e. something that tests and returns true/false. 'game' is the single argument to that function. From usage later on, it looks to be a map, or you might think of it as an associative array, or a key-value mapping. The first half tests whether the 'protection' value of the player, which itself is the 'player' value of the game (in C: game.player.protection, except those components are keys rather than fields) is less than (see the '<' operator on the left?) 3. The second half tests whether (I'm guessing) his experience level is greater than 15. The 'and' combines those, i.e. if he's at least a lvl 16 player and his prot value sucks at less than 3, he'll want protection (so return 'true').