Skip to content

Instantly share code, notes, and snippets.

View notyy's full-sized avatar

大魔头 notyy

View GitHub Profile
@notyy
notyy / gist:2828885
Created May 29, 2012 14:55
Writer with State
import Control.Monad
import Control.Monad.State
import Control.Monad.Writer
type User = String
type Log = [String]
login :: String -> State [User] ()
login u = do
modify (u:)
@notyy
notyy / gist:2823232
Created May 29, 2012 08:13
State monad
{-# LANGUAGE NoMonomorphismRestriction #-}
import Control.Monad.State
import Control.Monad.Identity
test1 = do -- when apply on init state 0
a <- get -- a = (0,0)
modify (+1) -- (0,1)
b <- get -- b = (1,1)
return (a,b) -- why result is ((0,1),1)
@notyy
notyy / gist:2819682
Created May 28, 2012 15:17
applicative
def connect[A,B,C](f1: A => B, f2: A => B, f3: B => B => C): (A => C) = x => {
f3(f1(x))(f2(x))
}
def add2 = (x:Int) => x + 2
def multi3 = (x:Int) => x * 3
def addThem = (x:Int) => (y:Int) => x + y
@notyy
notyy / gist:2601278
Created May 5, 2012 10:02
universe1
http://www.spoj.pl/problems/TEST/
import scala.io._
object Main {
def main(args: Array[String]) {
val source = Source.stdin
source.getLines.takeWhile((x=> x != "42")).foreach(println)
}
}
@notyy
notyy / gist:2595509
Created May 4, 2012 15:26
骑士问题
问题描述,有一个横8纵8的国际象棋棋盘,也就是坐标x,y都在(1..8)的范围内。
在棋盘的任意坐标上放一个马,马的走步规则,跟中国象棋差不多。。。如果不了解的话请谷歌之
现在给定棋盘上任意坐标,问这个马是否有可能精确的在第3步上走到该坐标?
=======================下面的解法译自haskell==================================
def moveKnight: (Int,Int) => List[(Int,Int)] = (c,r) => {
List((c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1),
(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)) filter (pos => (1 to 8).contains(pos._1) && (1 to 8).contains(pos._2))
}
import scala.io._
object Main {
def main(args: Array[String]) {
echo(Console.readLine)
}
def echo(line: => String): Unit = line match{
case "42" => ()
case (x:String) => println(x);echo(Console.readLine);
@notyy
notyy / gist:2516767
Created April 28, 2012 07:15
进制转换
import scala.math._
def toNum(base:Int, src: List[Int]): Int = (base, src) match {
case (_,Nil) => 0
case (b, (x::xs)) => x * pow(b,xs.length).toInt + toNum(b,xs)
}
=======================
scala> toNum(10,List(1,2,3,4))
res7: Int = 1234
@notyy
notyy / gist:2506584
Created April 27, 2012 06:31
quicksort in scala
def qs[T <% Ordered[T]](l: List[T]): List[T] = l match {
case Nil => Nil
case (x :: xs) => {
val (less,greater) = xs partition (_ <= x)
qs(less) ++ (x :: qs(greater))
}
}
@notyy
notyy / gist:2470138
Created April 23, 2012 10:31
random
def shuffle[T](t: Int, l: List[T]): List[T] = (t,l) match {
case (0, _) => Nil
case (x, Nil) => Nil
case (x, ls) => {
import scala.util.Random
val r = ls(Random.nextInt(ls.length))
r :: shuffle(x-1, ls.filterNot(item => item == r))
}
}
@notyy
notyy / gist:2468827
Created April 23, 2012 04:12
.emacs
(load "~/.emacs.d/haskell-mode/haskell-site-file")
(add-to-list 'load-path "~/.emacs.d/color-theme-6.6.0")
;; (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
;; (add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(require 'color-theme)
(color-theme-initialize)
(color-theme-hober)
(put 'downcase-region 'disabled nil)