view 7-Haskell/composition.hs @ 101:1fae0cca1ef8

Reduce large maze to single width corridors This reduces the permutations for a x x x b x To one (two steps north) from four (two steps north; one east, two north, one west; one east, one north, one west, one north; and one north, one east, one north, one west). Longer corridors were worse! We would filter this in the "been here before via another path" but that's still a lot of lookups in lists, which is inefficient.
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:42:24 +0100
parents 2b5341fc4555
children
line wrap: on
line source

module Composition where
    -- Haskell doesn't have a simple "subclass of Object" generic (https://stackoverflow.com/questions/6479444/is-there-a-type-any-in-haskell) but you can just use arbitrary values and not bind them to anything!
    -- The code in the book does this in the CLI and just avoids the signature definition
    second :: [o] -> o
    -- Here we use an implicit argument (so just the function to the left) which then becomes an implicit parameter to tail, which has head invokes on it due to function composition by "." (https://wiki.haskell.org/Function_composition)
    second = head . tail

    -- Fibonacci - like in fibonacci.hs, but without the index
    fib :: Integer -> Integer
    fib = fst . fibNthPair
    
    fibNextPair :: (Integer, Integer) -> (Integer, Integer)
    fibNextPair (x, y) = (y, x + y)

    fibNthPair :: Integer -> (Integer, Integer)
    fibNthPair 1 = (1, 1)
    fibNthPair n = fibNextPair (fibNthPair (n - 1))