Skip to content

Instantly share code, notes, and snippets.

import java.util.Arrays;
import java.util.List;
/**
* 検索クラス
*/
public class Search {
private static List<Person> persons = Arrays.asList(
new Person("Tarou", "Tokyo"),
import java.util.Arrays;
import java.util.List;
/**
* 検索クラス
*/
public class Search {
private static List<Person> persons = Arrays.asList(
new Person("Tarou", "Tokyo"),
import java.util.Arrays;
import java.util.List;
/**
* 検索クラス
*/
public class Search {
private static List<Person> persons = Arrays.asList(
new Person("Tarou", "Tokyo"),
import Data.List
data Person = Person { name, address :: String} deriving Show
ps = [ Person "Tarou" "Tokyo"
, Person "Jirou" "Osaka"
, Person "Saburou" "Nagoya"
]
getAddress n ps = case find ((== n) . name) ps of
module Counter where
-- カウンタを表わす型
data Counter a = Counter { val -- 値
, step :: a -- 増分
} deriving Show
-- カウンタの値を増加させる
next :: Num a => Counter a -> (a, Counter a)
next (Counter v s) = let v' = v + s
module State (State, comb, comb_, ret) where
type State s a = s -> (a, s)
comb :: State s a -> (a -> State s b) -> State s b
comb m n = \s0 -> let (x1, s1) = m s0
(x2, s2) = n x1 s1
in (x2, s2)
comb_ :: State s a -> State s b -> State s b
module Counter where
-- カウンタを表わす型
data Counter a = Counter { val -- 値
, step :: a -- 増分
} deriving Show
-- カウンタの値を増加させる
next :: Num a => Counter a -> (a, Counter a)
next (Counter v s) = let v' = v + s
module Stack(Stack, empty, empty', pop, push, push') where
newtype Stack a = Stack [a] deriving Show
empty :: Stack a
empty = Stack []
empty' :: Stack a -> ((), Stack a)
empty' _ = ((), Stack [])
module Main where
import State
import Counter
import Stack
next3 = next `comb_` next `comb_` next
push3 = push' 100 `comb_` push' 200 `comb_` push' 300
counter = Counter 0 1
stack = push 5 $ push 4 $ push 3 $ push 2 $ push 1 empty
class Boolean b where
isTrue :: b -> Bool
instance Boolean Bool where
isTrue True = True
isTrue False = False
instance Boolean Integer where
isTrue 0 = False
isTrue _ = True