Paste number 21967: simple Haskell problem

Index of paste annotations: 1 | 2 | 3

Paste number 21967: simple Haskell problem
Pasted by: Manyfold
2 years, 6 months ago
#math
Paste contents:
Raw Source | XML | Display As
module Main
   where

import IO

main = do
 

doInput list


doInput list =  do

   hSetBuffering stdin LineBuffering
   putStrLn "Please enter a number"
   num <- getLine
   list = num:List
   if num == 0
                then do computeSum list
                then do comute factorial list        
        else do doInput list

        
computeSum list = do
   
   erg = foldr (+) 0 list
   doOutput erg "Sum"

computefactorial = do
   
   erg = foldr (*) 1 list
   doOutput erg "Faktorial"

doOutput erg string = do

Annotations for this paste:

Annotation number 1: (for Manyfold) this could work
Pasted by: int-e
2 years, 6 months ago
Paste contents:
Raw Source | Display As
module Main
   where

import IO

main = do
   doInput []


doInput list =  do

   hSetBuffering stdin LineBuffering
   putStrLn "Please enter a number"
   num <- getLine
   let list' = num:list
   if num == 0
                then do computeSum list'
                        computeFactorial list'
        else do doInput list'

        
computeSum list = do

   let erg = foldr (+) 0 list
   doOutput erg "Sum"

computeFactorial = do
   
   let erg = foldr (*) 1 list
   doOutput erg "Faktorial"

doOutput erg string = do
   ...

Annotation number 2: (for Manyfold) with style changes; this one actually works.
Pasted by: int-e
2 years, 6 months ago
Paste contents:
Raw Source | Display As
module Main
   where

import IO

main = do
   hSetBuffering stdin LineBuffering
   list <- doInput []
   doOutput (sum list)     "Sum"
   doOutput (product list) "Faktorial"

doInput :: [Int] -> IO [Int]
doInput list =  do
   putStrLn "Please enter a number"
   num <- readLn
   let list' = num:list
   if num == 0 then return list
               else doInput list'

-- alternative idea for input:
doInput2 :: IO [Int]
doInput2 = do
   putStrLn "Please enter a number"
   num <- readLn
   if num == 0 then return []
               else do res <- doInput2
                       return (num:res)

doOutput erg str = do
   putStrLn $ str ++ " " ++ show erg

Annotation number 3: a solution to the problem
Pasted by: Cale
2 years, 6 months ago
Paste contents:
Raw Source | Display As
module Main
   where

import IO

main = do
   xs <- doInput []
   putStrLn $ "The sum is " ++ show (sum xs)
   putStrLn $ "The product is " ++ show (product xs)
   computeFactorials xs

doInput list = do
   hSetBuffering stdin LineBuffering
   putStrLn "Please enter a number"
   num <- readLn
   if num == 0
        then return list
        else doInput (list ++ [num])
 
factorial n = product [1..n]

computeFactorials [] = return ()
computeFactorials (n:ns) = do
    putStrLn $ (show n) ++ " factorial is " ++ (show (factorial n))
    computeFactorials ns

Colorize as:
Show Line Numbers
Index of paste annotations: 1 | 2 | 3

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.