2021-12-13 Haskell

https://www.jdoodle.com/execute-haskell-online/
https://replit.com/languages/haskell
https://tryhaskell.org/
https://www.seas.upenn.edu/~cis194/spring13/lectures/01-intro.html

Defining basic functions

-- Compute the sum of the integers from 1 to n.
sumtorial :: Integer -> Integer
sumtorial 0 = 0
sumtorial n = n + sumtorial (n-1)

arbitrary Boolean expressions

hailstone :: Integer -> Integer
hailstone n
  | n `mod` 2 == 0 = n `div` 2
  | otherwise      = 3*n + 1

Pairs

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

Pairs

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

Lists

nums, range, range2 :: [Integer]
nums   = [1,2,3,19]
range  = [1..100]
range2 = [2,4..100]
ex18 = 1 : []
ex19 = 3 : (1 : [])
ex20 = 2 : 3 : 4 : []
ex21 = [2,3,4] == 2 : 3 : 4 : []

cons operator, (:). Cons takes an element and a list, and produces a new list with the element prepended to the front.

你可能感兴趣的:(2021-12-13 Haskell)