Skip to content

Instantly share code, notes, and snippets.

@nabinno
Last active October 2, 2020 09:10
Show Gist options
  • Save nabinno/00c74e5af893785bb3c8258224ec2055 to your computer and use it in GitHub Desktop.
Save nabinno/00c74e5af893785bb3c8258224ec2055 to your computer and use it in GitHub Desktop.
はじめてPreludeでHaskellを試してみる
{-
$ nix-env --install ghc
$ ghc --version
/nix/store/z4ajipns0l1s8b2lrgpy6nng4cys7h99-bash-4.4-p23/bin/sh: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
The Glorious Glasgow Haskell Compilation System, version 8.8.3
$ ghci
/nix/store/z4ajipns0l1s8b2lrgpy6nng4cys7h99-bash-4.4-p23/bin/sh: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
/nix/store/z4ajipns0l1s8b2lrgpy6nng4cys7h99-bash-4.4-p23/bin/sh: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
GHCi, version 8.8.3: https://www.haskell.org/ghc/ :? for help
-}
Prelude> 1
1
Prelude> a
<interactive>:2:1: error: Variable not in scope: a
Prelude> "a"
"a"
Prelude> [1, 2, 3, 4]
[1,2,3,4]
Prelude> 'a'
'a'
Prelude> ['a', 'b', 'c']
"abc"
Prelude> "a"
"a"
Prelude> [1, 2, 3]** [1, 2, 3]
<interactive>:8:1: error:
* Non type-variable argument in the constraint: Floating [a]
(Use FlexibleContexts to permit this)
* When checking the inferred type
it :: forall a. (Floating [a], Num a) => [a]
Prelude> [1, 2, 3]== [1, 2, 3]
True
Prelude> [1, 2, 3] == [1, 2, 3]
True
Prelude> [1, [2, 3]] == [1, [2, 3]]
<interactive>:11:2: error:
* No instance for (Num [Integer]) arising from the literal `1'
* In the expression: 1
In the first argument of `(==)', namely `[1, [2, 3]]'
In the expression: [1, [2, 3]] == [1, [2, 3]]
Prelude> [[1], [2, 3]] == [[1], [2, 3]]
True
Prelude> [] == []
True
Prelude> [1] /= []
True
Prelude> [1..10]
[1,2,3,4,5,6,7,8,9,10]
Prelude> [1..100]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
Prelude> [1,3..100]
[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99]
Prelude> [1..]
Prelude> take 3 [1..10]
[1,2,3]
Prelude> take 3 [1..10]
[1,2,3]
Prelude> take 3 [1..]
[1,2,3]
Prelude> (1+)
<interactive>:22:1: error:
* No instance for (Show (Integer -> Integer))
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
Prelude> 1+3
4
Prelude> (+) 1 3
4
Prelude> (1 + ) 4
5
Prelude> map (1+) [1, 2, 3 ,4]
[2,3,4,5]
Prelude> map (+1) [1, 2, 3 ,4]
[2,3,4,5]
Prelude> (3+1)*2
8
Prelude> ((*2).(+1)) 3
8
Prelude> map ((*2).(+1)) [1..4]
[4,6,8,10]
Prelude> takeWhile (3>) [1..]
[1,2]
Prelude> dropWhile (3>) [1..10]
[3,4,5,6,7,8,9,10]
Prelude> [x | x <- [1..10], mod x 2 == 1]
[1,3,5,7,9]
Prelude> [x*2 | x <- [1..10], mod x 2 == 1]
[2,6,10,14,18]
Prelude> mod 5 3
2
Prelude> 5 `mod` 3
2
Prelude> (`mod` 3) 5
2
Prelude> map (`mod` 3) [1..10]
[1,2,0,1,2,0,1,2,0,1]
Prelude> map ((==0).(`mod` 3)) [1..10]
[False,False,True,False,False,True,False,False,True,False]
Prelude> filter ((==0).(`mod` 3)) [1..10]
[3,6,9]
Prelude> (if 1==1 then "OK" else "NG")
"OK"
Prelude> if True then "OK" else "NG"
"OK"
Prelude> if True then "OK" else ""
"OK"
Prelude> let factors x = [y | y <- [1..x], mod x y == 0]
Prelude> factors 5
[1,5]
Prelude> factors 12
[1,2,3,4,6,12]
Prelude> let isPrime x = length(factors x) == 2
Prelude> isPrime 5
True
Prelude> isPrime 1
False
Prelude> filter isPrime [1..20]
[2,3,5,7,11,13,17,19]
Prelude> let primes x = filter isPrime [1..x]
Prelude> primes 200
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment