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
my homie dhall
Dec 9, 2010

honey, oh please, it's just a machine

Eeyo posted:

This was kind of covered earlier with the haskell course from UPenn, but are there other sources of "homework assignments" in functional languages? I can read a book until I'm blue in the face but it's useless without something to work on. Nothing too big either, I don't want to write some giant program that I could have done in any other language, I just want to have the important features highlighted in the assignment. This is one of the reasons I didn't really like learn you a haskell/erlang. It talked about a lot of stuff but that's pretty much fluff, I would have preferred a helpful reference and some assignments.

I really liked Write Yourself a Scheme in 48 Hours

Adbot
ADBOT LOVES YOU

my homie dhall
Dec 9, 2010

honey, oh please, it's just a machine

tekz posted:

Thanks. I've noticed that the O'Reilly examples for simple stuff seems to include a lot of boilerplate. For example, for calculating the product of values in a list, instead of doing something simple like:

code:

  def product(acc \\ 1, [head | tail]) do
    if Enum.empty?(tail) do
      acc * head
    else
      product(acc* head, tail)
    end
  end

they recommend:

code:

  def product(list) do
    product(list, 1)
  end

  def product([], accumulated_product) do
    accumulated_product
  end

  def product([head | tail], accumulated_product) do
    product(tail, head * accumulated_product)
  end

Is their recommended way of doing stuff with using lots of repeated function definitions done for any kind of useful reason?

It is! Their implementation can run in constant space while yours, potentially, could blow up the stack. Check out the Wikipedia article on Tail Call Recursion/Optimization for more info.

my homie dhall
Dec 9, 2010

honey, oh please, it's just a machine
I'm looking at Idris.

Can anyone shed some light on what's going on here:
code:
data Fin : Nat -> Type where
   FZ : Fin (S k)
   FS : Fin k -> Fin (S k)
I think I understand what Fin 5 represents, but what's going on with these constructors??

my homie dhall
Dec 9, 2010

honey, oh please, it's just a machine

Ralith posted:

Remember that k : Nat is implicitly universally quantified, because it's lower case. Read the constructors as "for all Nats k, FZ inhabits Fin (S k)" and "for all Nats k, applying FS to a Fin k produces a Fin (S k)"


Asymmetrikon posted:

Fin n represents a finite set of size n (where n is greater than 0) - so like the natural numbers, but bounded by n.

FZ is the zero element, which is a member of all finite sets. You can think of its constructor as "for all natural numbers n, FZ is a member of Fin n iff n is greater than zero".

FS is the successor function; pretty much the same as the Nat successor.

Probably a point of confusion is that FZ and FS can inhabit multiple different types. FZ is a member of Fin 1, Fin 2, and so on. The finite sets are generated inductively, so Fin (S n) contains all of the elements of Fin n, plus FS applied on the greatest element. If we look at the members of each of the Fin instances, we see:

Fin 0 {}
Fin 1 {FZ}
Fin 2 {FZ, FS FZ}
Fin 3 {FZ, FS FZ, FS FS FZ}
...

So (FS FS FS FZ) is a member of Fins 4+.


xtal posted:

Do you notice a similarity between that type and the linked list? It's numbers represented at the type level by recursion. A function like 'length' on link lists could convert this type to an integer.

These were all pretty insightful, thanks!

  • Locked thread