Skip to content

Instantly share code, notes, and snippets.

lpfib :: Int -> (Int, [Int])
lpfib 0 = (1, [1])
lpfib 1 = (1, [1, 1])
lpfib n = aq `par` bq `pseq` (a + b, as ++ bs ++ [a + b])
where aq@(a, as) = lpfib (n - 1)
bq@(b, bs) = lpfib (n - 2)
-- por ejemplo
main = getArgs >>= mapM_ print . snd . lpfib . read . head
open Optimization
open System.Linq
type CheckPoint = {
CPId : string // checkpoint Id
Lat : double // latitude
Lon : double // longitude
}
type InterestPoint = {
#r @"C:\packages\FsControl.1.0.8\lib\net40\FsControl.Core.dll"
type Eval = Eval of int
let eval (Eval x) = x
// To define a Type Method we need to add at least 2 instances
type Eval' = Eval' of int
let eval' (Eval' x) = x
module ExpAlg =
@markhibberd
markhibberd / Aaa-notes.md
Last active August 29, 2015 13:59
lots of questions

So what I understand to be your questions:

  1. What is a coherency problem?
  2. What does over constained code look like / cause?
  3. How do you lose your "desired" instance?

A way to step through understanding this problem:

  • Oh shit, If I have local type classes, I have to handle crazy wacky cases in my implementation, this will likely have performance and correctness implications (see Coherency.scala)
  • What happens if I close over constraint on construction? Oops if I close over it, I end up with OverConstrained code (see OverConstrainedCode.scala) and worse I still have coherency issues, and the ability to lose my intended behavious (LosingAnInstance.scala)
  • Oh wow, if I just don't do local type classes, by never define conflicting implicits, and ascribe a single type to each behaviour, everything is simple and just works.

In the first Haskell Cast podcast Rein Henrichs and Chris Forno interviewed Edward Kmett in part about lens and it was suggested that Prisms don't have the same kind of introductory tutorial treatment. That's a shame, though. Prisms arise naturally all the time when using sum types.

You could have invented Data.Aeson.Lens (and maybe you already have)

I learned to use Prisms when using lens [1] with aeson so let us examine an extended use case to motivate using Prisms.

It's fairly common in dynamic languages to consume JSON like this

> it = json.loads('[{"someObject" : { "version" : [1, 0, 0] }}]')
@gusty
gusty / zipIndex.fsx
Last active August 29, 2015 14:01
Solve stack overflow in zipIndex function.
#r @"c:/packages/FsControl.1.0.9/lib/net40/FsControl.Core.dll"
open FsControl.Core.TypeMethods
open FsControl.Core.Types
open FsControl.Operators
type MonadBuilder() =
member inline b.Return(x) = result x
member inline b.Bind(p,rest) = p >>= rest
member b.Let (p,rest) = rest p
@Porges
Porges / typedtaglessfinal.cs
Created June 17, 2014 21:16
Encoding open recursion in C#: not recommended
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TypedTaglessFinal
{
module ATComposeProductSum where
import Control.Applicative
import Data.Traversable
import Data.Functor.Compose
import Data.Functor.Product
import Data.Functor.Sum
import Data.Functor.Identity
@bvenners
bvenners / gist:67cb4e8c0b285ca99fa3
Last active August 29, 2015 14:05
Getting a Scala compiler error when needed typeclass coherency is missing
scala> import org.scalactic._
import org.scalactic._
scala> val lower = EquiSets[String](StringNormalizations.lowerCased.toHashingEquality)
lower: org.scalactic.EquiSets[String] = EquiSets(NormalizingHashingEquality(lowerCased))
scala> val trimmed = EquiSets[String](StringNormalizations.trimmed.toHashingEquality)
trimmed: org.scalactic.EquiSets[String] = EquiSets(NormalizingHashingEquality(trimmed))
scala> val hiho = lower.EquiSet("hi", "ho")
@colinbull
colinbull / gist:4ec846e146f4ca15f3e1
Last active August 29, 2015 14:06
SqlProvider with ComposableQuery
open System
open FSharp.Data.Sql
open FSharpComposableQuery
FSharp.Data.Sql.Common.QueryEvents.SqlQueryEvent |> Event.add (printfn "Executing SQL: %s")
type HR = SqlDataProvider<ConnectionStringName = "***********", DatabaseVendor = Common.DatabaseProviderTypes.ORACLE>
let ctx = HR.GetDataContext()
type AuditRecord = {