筆記

Haskell 筆記

Week 1

sumer :: Integer -> Integer
sumer 0 = 0
sumer n = n + sumer (n-1)

collatz :: Integer -> [Integer]
collatz n
| n==1 = [1]
| mod n 2 ==0 = n : collatz (div n 2)
| otherwise = n : collatz (3*n+1)

foo :: Integer -> Integer
foo 0 = 16
foo 1
| "Haskell" > "C++" = 3
| otherwise = 4
foo n
| n<0 = 0
| mod n 17 == 2 = -43
| otherwise = n+3

a :: Char
a = '帥'

p :: (Int, Char)
p = (3, '夏')

f :: (Int, Int) -> Int
f (x, y) = x + y

g :: Int -> Int -> Int -> Int
g x y z = x + y + z

{- function application has 
higher precedence than any 
infix operators
-}

str1 :: [Char]
str1 = ['L', 'O', 'V', 'E']

str2 :: String
str2 = "LOVE"

emptyList = []
list1 = 1:[]
list2 = 2 : (1 :[])
list3 = 3 : 2 : 1 : []

nums, range1, range2 :: [Int]
nums = [1,2,4,6]
range1 = [2..10] -- 2,3,4,5,6,7,8,9,10
range2 = [3,6..18] -- 3,6,9,12,15,18
range3 = [3,6..19] -- 3,6,9,12,15,18 

main = print (collatz 3)

Ruby 筆記

  • 學習資源可以在 https://ruby.tw 找到。


Week 1