This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
问题描述,有一个横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)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |