A compilation of concepts I want to remember...

Navigation
 » Home
 » About Me
 » Github

journey with elm

09 Mar 2016 » elm

An attempt to log my journey through learning elm. Will work through tutorials found on the net, with an attempt to tweak each provided example so that I can get a better grip on the material. The examples will be in reverse chronological order, with the top displaying the most recent challenge.

Tutorials: \

  1. elm-by-example\
  2. cs223

A random walk, continuing on with the use of the Random module…


Estimating pi…

The approximation is based on the idea that: \[\lim_{n \to \infty} \frac{t}{n} = \frac{Area \ of \ Circle \ Quad}{Area \ of \ Circumscribed \ Square \ Quad} = \frac{\frac{\pi r^2}{4}}{r^2} = \frac{\pi}{4}\]


Admittedly took a bit of brute force to get this one done. Constrained the movement of the eyes by adjusting the related R values on the x,y-axis.


Learnt a bit about signals, and some work to get the coordinates to track mouse moves…


Instead of calculating and displaying the fibonacci sequence, I chose to pump out the sequence of perfect numbers up to 1000.

Perfect number, a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128. The discovery of such numbers is lost in prehistory. It is known, however, that the Pythagoreans (founded c. 525 bc) studied perfect numbers for their “mystical” properties.
-Encyclopedia Britannica

If anyone has suggestions on improving the properDivisor implementation please advise.

module PerfnumBars where

import Color exposing (lightBlue, lightGrey, lightPurple, orange, purple, red, yellow)
import Graphics.Collage exposing (collage, filled, rect)
import Graphics.Element exposing (down, flow, right ,show)
import List exposing ((::), map2, reverse, drop, head, length, map)
import Maybe exposing (withDefault)

properDivisors      : Int -> List Int
properDivisors n    = List.map(\(x,y) -> y) (List.filter (\(x,y) -> x == 0)
(List.map (\x -> (n % x, x)) [1..n]))

perfectNum          : Int -> List Int
perfectNum n        = List.filter (\ x -> List.sum (properDivisors x) // 2 == x)[1..n]

indexedPerfectNum   : Int -> List (Int, Int)
indexedPerfectNum n = map2 (,) [0..n] (perfectNum n)

color n =
    let colors = [lightBlue, lightGrey, lightPurple]
    in
        drop (n % (length colors)) colors |> head |> withDefault red

bar (index, n) =
    flow right [
        collage (n) 20 [ filled (color index) (rect (toFloat n) 20) ],
        show n
    ]

main = flow down <| map bar (indexedPerfectNum 1000)

Really no additional explanation required here, just changed the wording a bit.