fivemack: (Default)
[personal profile] fivemack
What do you mean, it's computationally foolish to define arithmetic using the Peano axioms?

>pinc [] = [1]
>pinc [1] = [0,1]
>pinc (0:u:xs) =
> if (head (pinc xs) == 0) then (u+head (pinc xs) : drop 1 (pinc xs))
> else (u : pinc xs)
>pinc (1:t:xs) = 0:t+1:xs
>pinc (u:xs) = 0:1:u-1:xs

>pnums = iterate pinc []

Date: 2005-03-31 11:47 pm (UTC)
From: [identity profile] hsenag.livejournal.com
Idiom: drop 1 == tail

Efficiency, not that you care:
Don't repeat calls to the same thing, there's no guarantee you'll get CSE.

More idiom: it's often nicer to use pattern matching than head/tail everywhere.

Algebra: if head (pinc xs) == 0 then u + head (pinc xs) == u

So...

pinc (0:u:xs) = case pinc xs of
(0:as) -> u:as
as@(_:_) -> u:as

March 2024

S M T W T F S
     12
3456789
10111213141516
17181920212223
24 252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 18th, 2025 08:37 pm
Powered by Dreamwidth Studios