Paste number 21967: simple Haskell problem

Index of paste annotations: 3 | 2 | 1

Paste number 21967: simple Haskell problem
Pasted by: Manyfold
When:3 years, 1 day ago
Share:Tweet this! | http://paste.lisp.org/+GY7
Channel:#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 3: a solution to the problem
Pasted by: Cale
When:3 years, 1 day ago
Share:Tweet this! | http://paste.lisp.org/+GY7#3
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

Annotation number 2: (for Manyfold) with style changes; this one actually works.
Pasted by: int-e
When:3 years, 1 day ago
Share:Tweet this! | http://paste.lisp.org/+GY7#2
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 1: (for Manyfold) this could work
Pasted by: int-e
When:3 years, 1 day ago
Share:Tweet this! | http://paste.lisp.org/+GY7#1
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
   ...

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

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