# Direct from Dell

Euler problem 9.2

.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

g p =
[ [a, b, c]
| m <- [2 .. limit],
n <- [1 .. (m - 1)],
let a = m ^ 2 - n ^ 2,
let b = 2 * m * n,
let c = m ^ 2 + n ^ 2,
a + b + c == p
]
where
limit = floor . sqrt . fromIntegral $p  — based on Haskell official . Euclid’s formula is a fundamental formula for generating Pythagorean triples given an arbitrary pair of integers $m$ and $n$ with $m > n > 0$. The formula states that the integers $\displaystyle{ a=m^{2}-n^{2},\ \,b=2mn,\ \,c=m^{2}+n^{2}}$ form a Pythagorean triple. The triple generated by Euclid’s formula is primitive if and only if $m$ and $n$ are coprime and one of them is even. When both $m$ and $n$ are odd, then $a$, $b$, and $c$ will be even, and the triple will not be primitive; however, dividing $a$, $b$, and $c$ by 2 will yield a primitive triple when $m$ and $n$ are coprime. Every primitive triple arises (after the exchange of $a$ and $b$, if $a$ is even) from a unique pair of coprime numbers $m$, $n$, one of which is even. — Wikipedia on Pythagorean triple — Me@2022-12-10 09:57:27 PM . . 2022.12.11 Sunday (c) All rights reserved by ACHK # Importance, 2.2 Euler problem 8.2 . . import Data.Char max13 lst = max13n lst 0 where max13n lst n | (length lst) < 13 = n | n > take13 = max13n (tail lst) n | otherwise = max13n (tail lst) take13 where take13 = product (take 13 lst) str <- readFile "n.txt" max13 (map (fromIntegral . digitToInt) . concat . lines$ str)


.

— Me@2022-11-19 12:04:41 PM

.

.

# Euler problem 6.2

1960s

.

— ShutUpImStillTalking

.

f = (sum [1..100])^2 - sum (map (^2) [1..100])


.

— colorized by palette fm

— Me@2022-11-03 05:55:51 PM

.

.

# The Sixth Sense, 2.2

Euler problem 5.2 | Folding an infinite list, 2

.

.

f = foldr1 lcm [1..20]

.

.

Most problems on Project Euler can be solved in three ways:

• with brute-force

• with an algorithm that solves a more general problem

• with a smart solution that requires pencil and paper at most

If you’re interested in a nice solution rather than fixing your code, try concentrating on the last approach …

— edited Oct 8, 2016 at 8:57

— huwr

— answered Dec 27, 2011 at 14:33

— Philip

— Stack Overflow

.

.

# Euler problem 4.2

.

Find the largest palindrome made from the product of two 3-digit numbers.

g = [(y, z, y*z) | y<-[100..999], z<-[y..999], f==y*z]
where
f = maximum [x | y<-[100..999], z<-[y..999],
let x=y*z, let s=show x, s==reverse s]

.

— Me@2022-10-10 10:09:53 PM

.

.

# SimCity 2013

Euler problem 3.4

.

.

primes = 2 : filter (null . tail . primeFactors) [3, 5 ..]

primeFactors n = factor n primes
where
factor n (p : ps)
| p * p > n = [n]
| n mod p == 0 = p : factor (n div p) (p : ps)
| otherwise = factor n ps

f = last (primeFactors 600851475143)


.

— Me@2022.09.28 11:44:20 AM

.

.

Euler problem 3.3

.

The goal of this blog post is to install an advanced Haskell mode, called LSP mode, for Emacs.

.

1. Open the bash terminal, use the following commands to install the three packages:

sudo apt-get install elpa-haskell-mode

sudo apt-get install elpa-yasnippet

sudo apt-get install elpa-which-key


.

2. Read and follow the exact steps of my post titled “Haskell mode“.

.

.

— Me@2022.09.20 12:49:29 PM

.

.

The goal of this blog post is to install an advanced Haskell mode, called LSP mode, for Emacs.

0. In this tutorial, you will need to go to the official website of NixOS and that of MELPA (Milkypostman’s Emacs Lisp Package Archive). Make sure that both websites are the real official ones. Any instructions from an imposter website can get your machine infected with malware.

1. Assuming your computer OS is Ubuntu 20.04 or above, go to the NixOS official website. Follow the instructions to install the Nix package manager (not the NixOS) onto your OS. Choose the “single-user installation” method.

2. On the NixOS official website, click the magnifying glass at the top right corner to reach the package search engine.

3. Search “haskell language server” and then copy its installation command.

nix-env -iA nixpkgs.haskell-language-server


4. Run the command in the bash terminal to install the Haskell Language Server.

.

5. Search “stack” on the package search engine.

6. Run its installation command

nix-env -iA nixpkgs.stack


to install the Haskell Tool Stack.

7. Search “ghc” on the package search engine.

8. Run its installation command

nix-env -iA nixpkgs.ghc


to install the Glasgow Haskell Compiler.

.

This step is needed for triggering the OS to recognize the Nix package manager setup.

.

10. Go to MELPA package manager’s official website. Follow the instructions to install “Melpa”, not “Melpa Stable”.

11. Open the Emacs editor. Click "Options" and then "Manage Emacs Packages".

Install the following packages. For each of them, make sure that you have chosen the source archive as “melpa“. Versions from other sources would not work.

company Modular text completion framework
flycheck On-the-fly syntax checking
lsp-mode LSP mode
lsp-ui UI modules for lsp-mode

12. Open Emacs’ initialization file, which has the filename

.emacs

Its location should be

~/.emacs


13. Add the following code to the file.

;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'company)

(require 'flycheck)

(require 'lsp-ui)

;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'lsp)

(save-place-mode 1)

;;;;;;;;;;;;;;;;;;;;;;;;;;

(interactive)

(windmove-up))

(global-set-key
(kbd "C-n")

;;;;;;;;;;;;;;;;;;;;;;;;;;


14. Close the Emacs program.

.

15. Create a dummy Haskell source code file named “test.hs”.

16. Use Emacs to open it.

17. You should see this message:

18. Select one of the first 3 answers. Then you can start to do the Haskell source code editing.

19. To compile your code, hold the Ctrl key and press n.

Ctrl+n


— Me@2022-08-18 05:22:02 PM

.

.

# Functional programming jargon in plain English

mjburgess 11 days ago | next [–]

These definitions don’t really give you the idea, rather often just code examples..
“The ideas”, in my view:

Monoid = units that can be joined together
Functor = context for running a single-input function
Applicative = context for multi-input functions
Monad = context for sequence-dependent operations
Lifting = converting from one context to another
Sum type = something is either A or B or C…
Product type = a record
= something is both A and B and C
Partial application = defaulting an argument to a function
Currying = passing some arguments later
= rephrasing a function to return a functions of n-1 arguments when given 1, st. the final function will compute the desired result
EDIT: Context = compiler information that changes how the program will be interpreted (, executed, compiled,…)
Eg., context = run in the future, run across a list, redirect the i/o, …

— Functional programming jargon in plain English

— Hacker News

.

Currying and partial function application are often conflated. One of the significant differences between the two is that a call to a partially applied function returns the result right away, not another function down the currying chain; this distinction can be illustrated clearly for functions whose arity is greater than two.

.

Partial application can be seen as evaluating a curried function at a fixed point, e.g. given $\displaystyle{f\colon (X\times Y\times Z)\to N}$ and $\displaystyle{a\in X}$ then

$\displaystyle{{\text{curry}}({\text{partial}}(f)_{a})(y)(z)={\text{curry}}(f)(a)(y)(z)}$

or simply

$\displaystyle{{\text{partial}}(f)_{a}={\text{curry}}_{1}(f)(a)}$

where $\displaystyle{{\text{curry}}_{1}}$ curries $\displaystyle{f}$‘s first parameter.

— Wikipedia on Currying

.

.

2022.07.16 Saturday ACHK

# Exercise 6.2

f :: a -> bf' :: a -> m aunit :: a -> m a

f' * g' = (bind f') . (bind g')

 bind f xs = concat (map f xs) 

bind unit xs = concat (map unit xs)

unit x = [x]

 

bind unit xs = concat (map unit xs)= concat (map unit [x1, x2, ...])= concat [unit x1, unit x2, ...]= concat [[x1], [x2], ...]= [x1, x2, ...]= xs

f' = lift f

 

lift f = unit . f

unit (or return) can directly act on an ordinary value only, but not on a monadic value. To act on a monadic value, you need to bind it.

How come we do not need to lift return?

f :: a -> b

 liftM :: Monad m => (a -> b) -> m a -> m b return :: a -> m a (liftM f) :: m a -> m b 

(>>=) :: Monad m => m a -> (a -> m b) -> m b

lifeM cannot be applied to return at all.

unit (or return) is neither a pure function nor a monadic function. Instead, it is an half-monadic function, meaning that while its input is an ordinary value, its output is a monadic value.

(bind return xs) -> ys

(bind return) applies to xs.

return applies to x.

liftM is merely fmap implemented with (>>=) and return

— Me@2016-01-26 03:05:50 PM

# Exercise 6a (Corrected version)

Show that f' * unit = unit * f' = bind f'

——————————

f :: a -> bf' :: a -> m aunit :: a -> m a

lift f = unit . ff' = lift f

The lift function in this tutorial is not the same as the liftM in Haskell. So you should use lift (but not liftM) with bind.

— Me@2015-10-13 11:59:53 AM

(f' * g') xs
= ((bind f') . (bind g')) xs

bind f' xs = concat (map f' xs)
unit x = [x]

bind unit xs = concat (map unit xs)= concat (map unit [x1, x2, ...])= concat [unit x1, unit x2, ...]= concat [[x1], [x2], ...]= [x1, x2, ...]= xs

(f' * unit) (x:xs)= bind f' (bind unit (x:xs))= bind f' (concat (map unit (x:xs)))= bind f' (concat (map unit [x1, x2, ...]))= bind f' (concat [[x1], [x2], ...])= bind f' [x1, x2, ...]= concat (map f' [x1, x2, ...])= concat [f' x1, f' x2, ...]= concat [(unit . f) x1, (unit . f) x2, ...]= concat [(unit (f x1)), (unit (f x2)), ...]= concat [[f x1], [f x2], ...]= [f x1, f x2, ...]

(unit * f') (x:xs)= ((bind unit) . (bind f')) (x:xs)= bind unit (bind f' (x:xs))= bind unit (concat (map f' (x:xs)))= bind unit (concat (map f' [x1, x2, ...]))= bind unit (concat [f' x1, f' x2, ...])= bind unit (concat [(unit . f)  x1, (unit . f) x2, ...])= bind unit (concat [(unit (f x1)), (unit (f x2)), ...])= bind unit (concat [[f x1], [f x2], ...])= bind unit [f x1, f x2, ...]= concat (map unit [f x1, f x2, ...])= concat [[f x1], [f x2], ...]= [f x1, f x2, ...]

— Me@2015-10-15 07:19:18 AM

If we use the identity bind unit xs = xs, the proof will be much shorter.

(f' * unit) (x:xs)= ((bind f') . (bind unit)) (x:xs)= bind f' (bind unit (x:xs))= bind f' (x:xs)

(unit * f') (x:xs)= ((bind unit) . (bind f')) (x:xs)= bind unit (bind f' (x:xs))= bind f' (x:xs)

— Me@2015-10-15 11:45:44 AM

# Exercise 6a

Show that f * unit = unit * f

——————————

(f * g) (x, xs)= ((bind f) . (bind g)) (x, xs)

bind f x = concat (map f x)

(f * unit) (x:xs)= bind f (bind unit (x:xs))= bind f (concat (map unit (x:xs)))= bind f (concat (map unit [x1, x2, x3, ...]))= bind f (concat ([[x1], [x2], [x3], ...]))= bind f [x1, x2, x3, ...]= concat (map f [x1, x2, x3, ...])= concat [f x1, f x2, f x3, ...]= [f x1, f x2, f x3, ...]

(unit * f) (x:xs)= ((bind unit) . (bind f)) (x:xs)= bind unit (bind f (x:xs))= bind unit (concat (map f (x:xs)))= bind unit (concat (map f [x1, x2, ...]))= bind unit (concat [f x1, f x2, ...])= bind unit [f x1, f x2, ...]= concat (map unit [f x1, f x2, ...])= concat [[f x1], [f x2], ...]= [f x1, f x2, ...]

— Me@2015.07.20 09:00 PM

# Exercise 3.2

Show that lift f * lift g = lift (f.g)

——————————

The meaning of f' * g' should be (bind f') . (bind g') instead.

f' * g' = (bind f') . (bind g')lift f = unit . ff' = lift f

(lift f * lift g) (x, xs)= (bind (lift f)) . (bind (lift g)) (x, xs)= bind (lift f) (bind (lift g) (x, xs))= bind (lift f) (gx, xs++gs)  where    (gx, gs) = (lift g) x

 = bind (lift f) (gx, xs++gs)  where    (gx, gs) = (g x, "") = bind (lift f) (g x, xs) = (fx, xs++fs)  where    (fx, fs) = (lift f) gx  = (fx, xs++fs)  where    (fx, fs) = (f gx, "") = (fx, xs)  where    (fx, fs) = (f (g x), "") 

= (f (g x), xs)

bind f' (gx,gs) = (fx, gs++fs)                  where                    (fx,fs) = f' gx

 bind (lift (f.g)) (x, xs)= (hx, xs++hs)  where    (hx, hs) = (lift (f.g)) x = (hx, xs++hs)  where    (hx, hs) = ((f.g) x, "") 

= ((f (g x)), xs)

— Me@2015.07.19 11:04 PM

# Exercise Three

Show that lift f * lift g = lift (f.g)

——————————

f' * g' = bind f' . g'lift f = unit . ff' = lift f

(lift f * lift g) (x, xs)= bind (lift f . lift g) (x, xs)= (hx, xs++hs)  where     (hx, hs) = lh x     lh x = (f' . g') x    f' = lift f    g' = lift g

This line does not work, since f' cannot be applied to (g' x), for the data types are not compatible:

f' :: Float -> (Float, String)

 g' :: Float -> (Float, String) 

(g' x) :: (Float, String)

The meaning of f' * g' should be bind f' . (bind g') instead.

— Me@2015-09-27 10:24:54 PM

# flatMap()

skybrian 70 days ago

If functional languages had called them the Mappable, Applicable, and FlatMappable interfaces, and used map(), apply(), and flatMap() instead of operators, it would have avoided a lot of confusion.

— Hacker News

bind ~ flatMap

— Me@2015-07-22 06:30:25 PM

2015.09.23 Wednesday ACHK

Monads in Haskell can be thought of as composable computation descriptions. The essence of monad is thus separation of composition timeline from the composed computation’s execution timeline, as well as the ability of computation to implicitly carry extra data, as pertaining to the computation itself, in addition to its one (hence the name) output, that it will produce when run (or queried, or called upon). This lends monads to supplementing pure calculations with features like I/O, common environment or state, etc.

2015.09.21 Monday ACHK

# Folding an infinite list

One big difference is that right folds work on infinite lists, whereas left ones don’t! To put it plainly, if you take an infinite list at some point and you fold it up from the right, you’ll eventually reach the beginning of the list. However, if you take an infinite list at a point and you try to fold it up from the left, you’ll never reach an end!

Learn You a Haskell for Great Good!

Note that the key difference between a left and a right fold is not the order in which the list is traversed, which is always from left to right, but rather how the resulting function applications are nested.

• With foldr, they are nested on “the inside”

foldr f y (x:xs) = f x (foldr f y xs)

Here, the first iteration will result in the outermost application of f. Thus, f has the opportunity to be lazy so that the second argument is either not always evaluated, or it can produce some part of a data structure without forcing its second argument.

• With foldl, they are nested on “the outside”

foldl f y (x:xs) = foldl f (f x y) xs

Here, we can’t evaluate anything until we have reached the outermost application of f, which we will never reach in the case of an infinite list, regardless of whether f is strict or not.

— edited Oct 24 ’11 at 12:21, answered Sep 13 ’11 at 5:17, hammar

— Stack Overflow

2015.08.23 Sunday by ACHK