プログラミング + アカデミック + 何か面白いこと

  1. Programming
  2. 30 view

[Haskell]Banana-Split Law part1

What I want to do

I want to implement an example of banana-split law.

・(|f|),(|g|)・=(|・f”F(π1),g”F(π2)・|)

Script

I didn’t know how to ouptut tuple with foldr, I quited to implement it.
After following implementation, collecting foldr for only one makes the script be an example of banana-split law.

-- Before
sum' = foldr (\x xs -> x + xs) 0
length' = foldr (\x n -> 1 + n) 0
average ls = sum' ls / length' ls

-- After
average' ls = s / l
    where
        (s, l) = ((foldr (\x xs -> x + xs) 0) ls, (foldr (\x n -> 1 + n) 0) ls)

Result

After all, I’ll run the script.

$ghci
Prelude> :load banana-strip.hs
Main> average' [1,2,3,4,5]
3.0

Programming recent post

  1. Install sbt 1.0.0 and run sample template

  2. [Machine Learning]Created docker image includ…

  3. [Node.js]How to write batch script with Node.…

  4. [Play][Scala]Develop Request Driven Batch Usi…

  5. [OpenCV][Ruby]Auto check web page design corr…

関連記事

PAGE TOP