Skip to content

Instantly share code, notes, and snippets.

View kritzcreek's full-sized avatar

Christoph Hegemann kritzcreek

View GitHub Profile
@kritzcreek
kritzcreek / Main.purs
Last active February 3, 2021 00:42
How to compile with continuations - Matt Might http://matt.might.net/articles/cps-conversion/
module Main where
import Prelude
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Effect (Effect)
import Effect.Console as Console
import Effect.Ref (Ref)
import Effect.Ref as Ref
@kritzcreek
kritzcreek / Main.purs
Created March 1, 2020 18:56
Set Interval in PureScript
module Main where
import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Console (log)
import Effect.Random as Random
import Effect.Ref as Ref
import Effect.Timer (clearInterval, setInterval)
@kritzcreek
kritzcreek / File.purs
Last active March 12, 2020 12:30
Purescript - Read a file from disc with the browser API
import Control.Monad.Aff (Aff, Canceler(..), makeAff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Exception (error)
import DOM (DOM)
import DOM.Event.EventTarget (addEventListener, eventListener, removeEventListener)
import DOM.File.FileReader as FR
import DOM.File.Types (Blob, fileReaderToEventTarget)
import DOM.HTML.Event.EventTypes as Events
import Data.Foreign (readString)
@kritzcreek
kritzcreek / Post.md
Last active January 20, 2019 22:42
Teaching Polymorphism

"Is Haskell a good first programming language?"

This question was asked at the recent HaskellX conference, with both practicioners as well as teachers being present. I won't reiterate everything that was said, and instead link to a recording of the podium discussion (https://skillsmatter.com/skillscasts/10952-park-bench-panel-session-with-haskellx-experts).

One particular argument, that I've heard come up multiple times now, is that teachers are complaining about the length function being polymorphic. Apparently length :: forall a. Foldable f => f a -> Int is hard to grasp/understand/accept for a new-comer. The solutions proposed to solve this problem are to:

  1. Return length to its non-Foldable type
  2. Use a custom Prelude to teach beginners

Drawbacks for 1 include that practicioners like the fact that length works for all kinds of data structures, and don't want that taken away from them. The primary problem with solution 2 is that students can't go and use their aquired knowledge to work on Open

@kritzcreek
kritzcreek / random-numbers.md
Last active May 24, 2021 18:36
Collections of random numbers in PureScript

Generating collections of random numbers in PureScript

A problem that I've seen beginners run into in Haskell or PureScript a couple of times now is how to generate a List of random numbers. It's a common requirement for little games (which make for great first projects) to generate these, and it definitely seems to be a stumbling block.

Why are random numbers hard?

Randomness is considered a side effect in purely functional languages, which means that to generate them you usually need access to Eff/IO, which in turn means we need to deal with Monads. And while generating a single random number is usually pretty easy with do-notation, the typical intuition beginners have built when going from single values to a collection is to use map, but that fails.

Type-Directed-Search to the rescue

@kritzcreek
kritzcreek / Kinds-and-do-syntax.md
Last active March 28, 2020 11:51
Kinds and Do-Syntax

Kinds

In PureScript there are types of different kinds. Kinds are types for types. For example Int has kind Type, and we write it as Int :: Type. You can ask for the kind of a type in purs psci

> :k Int
Type

Type constructors take types to other types. For example Array (which still needs another type to form a type a value could have, like Array Int):

@kritzcreek
kritzcreek / Main.purs
Last active March 6, 2016 15:36
Space Invader
module Main where
import Prelude
import Data.Array ((..), length, head, zip)
import Data.Foldable (fold, foldMap)
import Data.Int (toNumber)
import Data.Maybe (Maybe(Just), maybe)
import Data.Monoid (mempty)
import Data.String as S