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.
 
  • Locked thread
Votlook
Aug 20, 2005
You can just hardcode it like this.

code:
days = [Mon..Sun]
You can use the [from..to] syntax here because Day is an instance of Enum.
For a more generic solution make your enum type make an instance of Bounded like so:

code:
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving (Eq, Enum, Bounded, Show)
This provides an implementation of minBound and maxBound (Mon and Sun in this case)
You can then use the minBound and maxBound to define the list of all enum elements:

code:
enumElems = [minBound .. maxBound ]
Note that is is a generic list for all types that are an instance of both Enum and Bounded, eg:

code:
enumValues :: (Bounded t, Enum t) => [t]
If Haskell can't infer the 't', Day in your example, you'll need to provide the type like so

code:
enumElem :: [Day]
You can then use the map function to do stuff to each element, like so:

code:
map show (enumValues :: [Day])
This works for any type that implements Enum and Bounded, so

code:
data PrivatePart = Dick | Balls deriving (Eq, Enum, Bounded, Show)
map show (enumValues :: [PrivatePart])
will just show each private part.

Votlook fucked around with this message at 21:18 on Sep 28, 2015

Adbot
ADBOT LOVES YOU

Votlook
Aug 20, 2005

Barnyard Protein posted:

Does anyone know of a racket function/macro that does something like these clojure operators?

http://clojuredocs.org/clojure.core/-%3E
http://clojuredocs.org/clojure.core/-%3E%3E

[/code]

There is no built in macro that does this in Racket, but I know at least one library that provides it, Rackjure
http://www.greghendershott.com/rackjure/index.html#%28part._.Threading_macros%29

Votlook
Aug 20, 2005

Pollyanna posted:


I feel like I'm doing something horribly wrong here. There's lets everywhere, a whole bunch of code repetition, and overall the whole thing feels way too verbose and weird. I tried to take a top-down approach with programming the game, but I feel like I totally missed the mark. I'm not entirely sure if my approach of making Player and Card records was a good one, and I don't feel like I'm really taking advantage of Clojure's strengths here.

How can I improve this code, and the project in general? How would someone who's better at programming in Lisp than I am approach a problem like this? Does what I'm doing make sense, or is it in need of serious refactoring?

Some hints:

You can use the update and update-in functions to modify entries directly:

Lisp code:
(defn play-war-cards [game-state]
  (-> game-state
      (update :player-1 p/play-war-card)
      (update :player-2 p/play-war-card)))
You can destructure argument directly, no need to using let for that:

code:
(defn play-war-cards [{:keys [player-1 player-2] :as game-state}]
  ...)

Votlook
Aug 20, 2005

Shinku ABOOKEN posted:

can someone motivate me to learn clojure? what can it do better? a demonstration would be nice.

i keep telling myself to learn it but i cant seem to find value in it :(

It will improve your Java stacktrace reading skills

Seriously though, my main reason for learning Clojure was the interactive development style it allows, with the REPL you
can inspect and incrementally change stuff in a running application, which is way more fun than the edit/compile/run cycle most languages offer.
It also has nice datastructures that can be read & printed easily, which is super nice for debugging.

The syntax is also awesome. The parentheses are weird at the beginning, some editors are capable of editing S-expressions directly, (im using Emacs with the paredit plugin) the learning curve is big, but once you get the hang of this, the parentheses are actually an advantage, as they give you a way to edit
based on the structure of the code. See a demo here: https://www.youtube.com/watch?v=D6h5dFyyUX0

  • Locked thread