Skip to content

Commit e2c9033

Browse files
committed
Haskell is really a mindblowing game :)
1 parent 87dcf4c commit e2c9033

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
**TODO**.
1616
- [What I Wish I Know When Learning Haskell](http://dev.stephendiehl.com/hask),
1717
**TODO**.
18+
- [CS240H: Functional Systems in Haskell](http://www.scs.stanford.edu/16wi-cs240h),
19+
**TODO**.
1820

1921

2022
### Random explorations

lyh04.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ howLong xs = "This list is " ++ long xs ++ " long"
3131
long [_] = "one"
3232
long [_, _] = "two"
3333
long _ = "too"
34+
-- Since `case' is an expression, doing so is also fine,
35+
-- despite its less readability
36+
howLong' xs = "This list is " ++
37+
case xs of
38+
[] -> "zero"
39+
[_] -> "one"
40+
[_, _] -> "two"
41+
_ -> "too"
42+
++ " long"

lyh06.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ merge = map (\(a, b) -> a + b)
3030
sum' :: Num a => [a] -> a
3131
sum' = foldl (+) 0
3232
elem' x xs = foldl (||) False (zipWith (==) (repeat x) xs)
33+
-- A possibly clearer method
34+
elem'' x xs = any (==x) $ xs
3335
-- `map' can also built upon `foldl', but it requires `++',
3436
-- which it much more expansive than `:'
3537
map' f xs = foldr applyNew [] xs

rwh04.hs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
-- RWH Chapter 4. Functional programming --
2+
3+
import Control.Exception (assert)
4+
5+
-- Better to use `null' than `length', because of the lazy evaluation
6+
-- Or, pattern matching is also a good idea (which actually resembles
7+
-- what `null' does, so less readable)
8+
-- This sucks on infinite lists, also decrease performance on long lists
9+
safeHeadDumb xs = if length xs > 0
10+
then Just $ head xs
11+
else Nothing
12+
safeHead xs
13+
| null xs = Nothing
14+
| otherwise = Just $ head xs
15+
safeHead' [] = Nothing
16+
safeHead' xs = Just $ head xs
17+
18+
-- Partial / total functions (think of `totality' in Idris)
19+
20+
-- `and', `or', `all', `any' on empty list
21+
-- Be careful! Assertion is done when you need its value!
22+
andEmpty = assert (and []) True
23+
orEmpty = assert (not $ or []) False
24+
25+
26+
27+
-- Failed to get a reasonably working `zipWithN'
28+
-- Further readings:
29+
-- * http://okmij.org/ftp/Haskell/polyvariadic.html
30+
-- * https://stackoverflow.com/questions/20558648/what-is-the-datakinds-extension-of-haskell
31+
-- * https://www.reddit.com/r/haskell/comments/b9qyp/generalized_zipwithn_with_a_pretty_implementation/
32+
33+
{-
34+
--data Z
35+
--data S n
36+
37+
class NatureNum a where
38+
fromNum :: Integral b => b -> a
39+
toNum :: Integral b => a -> b
40+
41+
--data Nat n where
42+
-- Zero :: Nat Z
43+
-- Succ :: Nat n -> Nat (S n)
44+
data Nat = Z | S Nat
45+
46+
--instance NatureNum (Nat) where
47+
-- toNum Z = 0
48+
-- toNum (S n) = 1 + toNum n
49+
50+
--instance NatureNum Zero where
51+
-- fromNum _ = Zero
52+
-- toNum _ = 0
53+
--instance NatureNum (Succ n) where
54+
-- fromNum _ = Succ Zero
55+
-- toNum _ = 1
56+
57+
--fromNum :: Integral a => a -> (Nat n)
58+
--fromNum 0 = Zero
59+
----fromNum k = Succ ( fromNum (k-1))
60+
--fromNum 1 = Succ Zero
61+
62+
63+
--data NArgsFunc a :: Nat -> * where
64+
-- NArgsFunc ::
65+
66+
--data NArgsFunc a = ZeroArgFunc a
67+
-- | NArgsFunc a (NArgsFunc a)
68+
69+
type ZeroArgFunc a = a
70+
testZ :: (ZeroArgFunc Int)
71+
testZ = (42)
72+
type NArgsFunc Nat a = a -> (NArgsFunc Z
73+
test1 :: (NArgsFunc [Char] (ZeroArgFunc Int))
74+
test1 = length
75+
-}

0 commit comments

Comments
 (0)