[1..10] ‥→ (1 to: 10) asArray filter isLower "aAbBcC" ‥→ 'aAbBcC' select: [ :each | each isLowercase ] " ==> 'abc' " foldr (+) 0 [1..10] ‥→ (1 to: 10) inject: 0 into: [ :sum :each | sum + each ] " ==> 55 " read "3" + 4 ‥→ '3' + 4 あるいは、(Number readFromString: '3') + 4 " ==> 7 " (read "3")::Int ‥→ Integer readFromString: '3' " ==> 3 " (read "3")::Float ‥→ Float readFromString: '3' " ==> 3.0 " (\x -> x * x) 3 ‥→ [ :x | x * x ] value: 3 " ==> 9 "
fact 1 = 1 fact n = n * fact (n-1) : ↓ Integer >> fact ^ self = 1 ifTrue: [1] ifFalse: [self * (self - 1) fact]
qsort [] = [] qsort (h:t) = qsort (filter (< h) t) ++ [h] ++ qsort (filter (>= h) t) あるいは、 qsort [] = [] qsort (h:t) = qsort [ x | x <- t, x < h ] ++ [h] ++ qsort [ x | x <- t, x >= h ] : ↓ ArrayedCollection >> qsort | h t | self size < 2 ifTrue: [^ self]. h := self first. t := self allButFirst. ^ (t select: [ :x | x < h ]) qsort, {h}, (t select: [ :x | x >= h ]) qsort
上にも追記しましたが、手続き(あるいは、無名関数)オブジェクトとしての[ toUpper x | x <- "aXbYcZ", isLower x ] : ↓ 'aXbYcZ' select: [ :x | x isLowercase ] thenCollect: [ :x | x asUppercase ] " ==> 'ABC' "
--sumimtoHexString n | n < 16 = [toHexDigit n] | otherwise = toHexString (div n 16) ++ [toHexDigit (mod n 16)] where toHexDigit x = "0123456789ABCDEF" !! x : ↓ Integer >> asHexDigit ^ '0123456789ABCDEF' at: self + 1 Integer >> asHexString self < 16 ifTrue: [^ self asHexDigit asString]. ^ (self // 16) asHexString, (self \\ 16) asHexDigit asString あるいは、 Integer >> asHexString | toHexDigit | toHexDigit := [ :x | '0123456789ABCDEF' at: x + 1 ]. self < 16 ifTrue: [^ (toHexDigit value: self) asString]. ^ (self // 16) asHexString, (toHexDigit value: self \\ 16) asString 5295 asHexString " ==> '14AF' " ('16r', 5295 asHexString) asNumber " ==> 5295 "
残念ながら、Hugs では zip の要素数の不一致でエラーがでちゃうけど、fib = 1:1:[ a+b | (a,b) <- zip fib (tail fib) ]
--sumimnewtype Natural = MakeNatural Integer toNatural :: Integer -> Natural toNatural x | x < 0 = error "Can't create negative naturals!" | otherwise = MakeNatural x fromNatural :: Natural -> Integer fromNatural (MakeNatural i) = i instance Num Natural where fromInteger = toNatural x + y = toNatural (fromNatural x + fromNatural y) x - y = let r = fromNatural x - fromNatural y in if r < 0 then error "Unnatural subtraction" else toNatural r x * y = toNatural (fromNatural x * fromNatural y) : ↓ Reading file "natural.hs": Type checking ERROR "natural.hs":10 - Cannot build superclass instance *** Instance : Num Natural *** Context supplied : () *** Required superclass : Eq Natural
というように最初の newtype のところに deriving (Show, Eq) を追加すればOKだったみたいです。newtype Natural = Natural Integer deriving (Show, Eq) toNatural :: Integer -> Natural toNatural x | x < 0 = error "Can't create negative naturals!" | otherwise = Natural x fromNatural :: Natural -> Integer fromNatural (Natural i) = i instance Num Natural where fromInteger = toNatural x + y = toNatural (fromNatural x + fromNatural y) x - y = let r = fromNatural x - fromNatural y in if r < 0 then error "Unnatural subtraction" else toNatural r x * y = toNatural (fromNatural x * fromNatural y)
こちらも思惑通り、うまくゆきました。--sumimfact :: Natural -> Natural fact 0 = 1 fact n = n * fact (n - 1)
とかできると万々歳なのですが、残念ながらそうは問屋は卸さないようで…。fact :: Natural -> Integer fact 0 = 1 fact n = n * fact (n - 1)
などとして、返値の Integer を保証してやらなあかんようです。fact :: Natural -> Integer fact n = fromNatural (nfact n) where nfact 0 = 1 nfact n = n * nfact (n - 1)
このページを編集 (10473 bytes)
以下の 5 ページから参照されています。 |
This page has been visited 9495 times.