Memo List May
· 阅读需 4 分钟
少量尚不能单独成篇的笔记
Week13
[2017-05-18 19:15:40]
## R-lang `data frame with 0 columns and 0 rows error`
Week14
[2017-05-27 12:38:29]
将字符串型数字转成[int]
Integer -> [Int]
toDigits :: Integer -> [Int]
toDigits = map (fromIntegral . digitToInt) . show
获取随机数/随机数的list
randint :: IO Int
randint = randomRIO (1,55) -- 生成int的范围
-- 获得一个长为 10 的list
rint <- sequence (replicate 10 randint)
randomList :: IO [Int] -- 返回一个 generater
randomList = getStdGen >>= return . randomRs (1,55)
Ruby Random String
- How to generate a random string in Ruby
- Generate Unique Random String With Letters And Numbers In Lower Case
- How do I populate an array with random numbers?
- How do I pick randomly from an array?
以下的shuffle
均可改成shuffle(random: Random.new)
以提高随机性
生成len
长度的大小写字母
([*('a'..'z'),*('A'..'Z')]*len).shuffle[0,len].join
生成len
长度的小写字母+数字
(36**(len-1) + rand(36**len - 36**(len-1))).to_s(36)
生成len ± 1
长度的小写字母+数字
rand(36**len).to_s(36)
生成 随机长度 随机内容的 [[_RandomString], [_RandomString], ...]
def gen_test_case # for "Sort array by string length" Ruby Translation —— codewars
#prng = Random.new # 使用新得随机数生成器
arr = [*(1..10)].
shuffle[0, rand(1..100)]. # arr.length
sort.
#map{|len| ([*('a'..'z'),*('A'..'Z')]*len).shuffle[0,len].join}
map{|len| (36**(len-1) + rand(36**len - 36**(len-1))).to_s(36)}
[arr.shuffle(random: Random.new), arr]
# arr 为按长度排序的 数组
end
[2017-05-23 12:03:29]
[2017-05-22 19:25:21]
Real World Haskell 中文版
修改提示符
Note: 提示符并不需要引号包起来。[ghci ver 8.0.1]
Prelude> :set prompt "ghci>"
"ghci>" :set prompt ghci>
ghci>
基本算术运算
ghci>2+2
4
ghci>2/2
1.0
ghci>7.0/2
3.5
ghci>331*2
662
ghci>123*0.5
61.5
ghci>123*1.0
123.0
前缀表达式
ghci>(+) 2 2
4
ghci>(/) 10 5
2.0
幂
ghci>2^3
8
ghci>2**3
8.0
负数
负数需要用括号括起来。单独出现时除外,此时-
为一元运算符。
ghci>-1
-1
ghci>2* -1
<interactive>:18:1: error:
Precedence parsing error
cannot mix ‘*’ [infixl 7] and prefix `-' [infixl 6] in the same infix expression
ghci>2* (-1)
-2
Week15
[2017-06-02 22:20:48]
Haskell 可选参数(optional arguments)
1 中的第一个答案可以用,后面的跑不起来
{-# LANGUAGE FlexibleContexts #-}
multiProduct req1 opt1 opt2 opt3 = req1 * opt1' * opt2' * opt3'
where opt1' = fromJust (fromMaybe 10 opt1)
opt2' = fromJust (fromMaybe 20 opt2)
opt3' = fromJust (fromMaybe 30 opt3)
multiProduct' req1 opt1 opt2 opt3 = req1 * opt1' * opt2' * opt3'
where opt1' = if isJust opt1 then (fromJust opt1) else 10
opt2' = if isJust opt2 then (fromJust opt2) else 20
opt3' = if isJust opt3 then (fromJust opt3) else 30
Error
ghci> multiProduct 1
<interactive>:215:1: error:
? Non type-variable argument in the constraint: Num (Maybe a)
(Use FlexibleContexts to permit this)
? When checking the inferred type
it :: forall a.
(Num (Maybe a), Num a) =>
Maybe (Maybe a) -> Maybe (Maybe a) -> Maybe (Maybe a) -> a
ghci> multiProduct' 1
<interactive>:216:1: error:
? No instance for (Show (Maybe a0 -> Maybe a0 -> Maybe a0 -> a0))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
? In a stmt of an interactive GHCi command: print it
Tips
[2017-05-29 11:12:58]