Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- I just came up with a generalFold.
- -- Is there something similar in Haskell?
- generalFold :: ((info,acc) -> Maybe (info,acc)) -> (info,acc) -> (info,acc)
- generalFold transition duple =
- let newDupleMaybe = transition duple
- in case newDupleMaybe of
- Nothing -> duple
- Just newDuple -> generalFold transition newDuple
- sum10 = generalFold transition ([1..10],0)
- where
- transition (info,acc) =
- if (length info) <= 0 then Nothing
- else Just (tail info,(head info)+acc)
- product10 = generalFold transition ([1..10],1)
- where
- transition (info,acc) =
- if (length info) <= 0 then Nothing
- else Just (tail info,(head info)*acc)
- vectorToMatrix :: [Double] -> Int -> [[Double]]
- vectorToMatrix vector nColumns =
- let (info,acc) = generalFold transition (vector,[])
- in acc
- where
- transition (info,acc) =
- if n < 0 then Nothing
- else Just (take n info,(drop n info):acc)
- where n = length info - nColumns
- matrix3x3 = vectorToMatrix [1..9] 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement